

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 4
click for more info
Not enough gems
Cost: 6 gems
1: Big O Notation
incomplete
2: O(n) - Order 'n'
incomplete
3: O(n^2) - Order 'N Squared'
incomplete
4: N^2 Quiz
incomplete
5: O(nm)
incomplete
6: Constants Don't Matter
incomplete
7: Constants Quiz
incomplete
8: Order 1
incomplete
9: Order Log N
incomplete
10: Name Count
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
O(n^2) grows in complexity much more rapidly. That said, for small and medium input sizes, these algorithms can still be very useful.
A common reason an algorithm falls into O(n^2) is by using a nested loop, where the number of iterations of each loop is equal to the number of items in the input:
for person_one in persons:
for person_two in persons:
# every combination of people
# will go on a date... twice!
go_on_date(person_one, person_two)
LockedIn needs search capabilities! For now, we'll build something slow (and frankly awful) so we can see an n^2 algorithm in practice.
Complete the does_name_exist function.
When you run your completed code, notice how each successive call to does_name_exist takes quite a bit longer. Assuming the length of first_names and last_names is the same, each new name doesn't add n steps to the algorithm; the total number of steps grows quadratically with the size of the input, making the total work O(n^2).
If does_name_exist(10 names, 10 names) takes just 1 second to complete, then we can estimate:
does_name_exist(100 names, 100 names) = 100 secondsdoes_name_exist(1000 names, 1000 names) = 10,000 secondsdoes_name_exist(10000 names, 10000 names) = 1,000,000 seconds