Counting comparisons: list scan versus set lookup
The numbers are operation counts, not times, so they are the same on every machine.
The Python idioms that save minutes in a coding round: Counter, defaultdict, heapq, bisect, deque, slicing, sorted with key, and itertools, plus the time complexity of every built-in you will lean on and how to talk through a solution.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write group_anagrams(words) that returns a list of groups, each sorted, with the groups sorted too.
Write kth_largest(nums, k) using heapq in O(n log k), keeping a min-heap of size k.
Write first_unique_char(s) that returns the index of the first character that appears only once, or -1.
Explain it without notes
Find the k most frequent words in a text, with ties broken alphabetically. State the time and space complexity.
Why is list.pop(0) slow and what do you use instead? What is the complexity of x in some_list versus x in some_set?
What is a stable sort, and how do you sort by one field descending and another ascending?
How does bisect_left differ from bisect_right? Give an example with duplicates.
How would you find the maximum of every window of size k in an array in O(n)?
Given a list of numbers and a target, return the indexes of two numbers that add up to the target, in O(n) time.
What goes wrong when you remove items from a list while looping over it, and what do you do instead?
Expected output
n=10 list: 10 set: 1
n=100 list: 100 set: 1
n=1000 list: 1000 set: 1
same result: True