1.7k

# Computer Science Fundamentals for Self-Taught Programmers by Justin Abrahms

April 11, 2014

## Transcript

2. ### Who am I? • Justin Abrahms • Director of Product

Engineering at Quick Left • Author of Imhotep • @justinabrahms or github://justinabrahms
3. ### Overview • How I learn about Big O? • What

is Big O? • How is Big O done? • Resources and learned wisdom
4. None

8. ### What does N+1 Selects look like? entries = get_post_ids() final_list

= [] for entry_id in entries: entry = get_entry(entry_id) final_list.append(entry)
9. ### Questions • How did I miss this? • How did

this guy know about it and I didn’t? • How can I make sure this never happens again.
10. ### –Wikipedia (aka Inducer of Impostor Syndrome) In mathematics, big O

notation describes the limiting behavior of a function when the argument tends towards a particular value or inﬁnity, usually in terms of simpler functions.
11. ### –Me Big O is how programmers talk about the relation

of how much stuff is being done when comparing two pieces of code.
12. ### Google Study List Studied: • Data Structures • Algorithms •

System Design • Java Internals • Concurrency Issues
13. ### Google Study List Studied: • Data Structures! • Algorithms! •

System Design • Java Internals • Concurrency Issues These are Big O things
14. ### –Wikipedia A data structure is a particular way of storing

and organizing data in a computer so that it can be used efﬁciently.

17. ### What sorts of Big O are there? • O(1) —

Constant Time • O(log n) — Logarithmic Time • O(n) — Linear Time • O(n²) — Quadratic Time • O(n!) — Factorial Time

20. ### def item_in_list(item, lst): for entry in lst: if entry ==

item: return True return False O(n)

22. ### def item_in_list(item, lst): for entry in lst: if entry ==

item: return True return False O(n) — Broken Down
23. ### def item_in_list(item, lst): for entry in lst: O(n) if entry

== item: return True return False O(n) — Broken Down
24. ### def item_in_list(item, lst): for entry in lst: O(n) if entry

== item: O(1) return True return False O(n) — Broken Down
25. ### def item_in_list(item, lst): for entry in lst: O(n) if entry

== item: O(1) return True O(1) return False O(1) O(n) — Broken Down
26. ### def item_in_list(item, lst): for entry in lst: O(n) if entry

== item: O(1) return True O(1) return False O(1) O(n) — Broken Down =O(n) * O(1) + O(1)

+ O(1)?
28. ### In non-“math-y” terms • If we plot our function, we

can also plot M * the big O and end up with a line that our function never crosses (for certain values of X)

?
30. ### Example O(n) * O(1) + O(1) Big O: To Plot:

x * ? O(n) always means x
31. ### Example O(n) * O(1) + O(1) Big O: To Plot:

x * 5 + 9 O(1) means pick any constant number

38. ### def item_in_list(item, lst): for entry in lst: if entry ==

item: return True return False O(n)

40. ### def item_in_list(item, lst): for entry in lst: if entry ==

item: return True return False O(n)

search.

9 10

9 10 4

9 10 4

9 10 4 == 6?
46. ### O(log n) 1 2 3 4 5 6 7 8

9 10 4 == 6? Nope.

9 10 4

9 10 4

9 10 4 == 3?
50. ### O(log n) 1 2 3 4 5 6 7 8

9 10 4 == 3? Nope.

9 10 4

9 10 4

9 10 4 == 4?
54. ### O(log n) 1 2 3 4 5 6 7 8

9 10 4 == 4? Yes!
55. ### def get_pairs(lst): pair_list = [] for i1 in lst: for

i2 in lst: pair_list.append([i1, i2]) return pair_list ! O(n²)
56. ### def get_pairs(lst): pair_list = [] O(1) for i1 in lst:

O(N) for i2 in lst: O(N) pair_list.append([i1, i2]) O(1) return pair_list O(1) ! O(n²)
57. ### def get_pairs(lst): pair_list = [] O(1) for i1 in lst:

O(N) for i2 in lst: O(N) pair_list.append([i1, i2]) O(1) return pair_list O(1) ! O(n²) = O(1) + O(n) * O(n) * O(1) + O(1)

O(1)

O(1)
60. ### O(n²) = O(1) + O(n) * O(n) * O(1) +

O(1) = O(n) * O(n) * O(1) + O(1) + O(1)
61. ### O(n²) = O(1) + O(n) * O(n) * O(1) +

O(1) = O(n) * O(n) * O(1) + O(1) + O(1) = x * x + 7 + 9 + 13
62. ### O(n²) = O(1) + O(n) * O(n) * O(1) +

O(1) = O(n) * O(n) * O(1) + O(1) + O(1) = x * x + 7 + 9 + 13 = x² + 29

77. ### Big O is… • useful in communicating about complexity of

code • basic arithmetic and algebra • used in talking about algorithms and data structures • not as hard as it originally sounds
78. ### Thanks • justin@abrah.ms • @justinabrahms • github.com/justinabrahms Credits: ! NYC

slide photo via ﬂickr://Andos_pics