Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 17.4 — Interview Deep Dive: Coding Rounds in Python
Python · Example 3 of 3

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.

01

Write group_anagrams(words) that returns a list of groups, each sorted, with the groups sorted too.

02

Write kth_largest(nums, k) using heapq in O(n log k), keeping a min-heap of size k.

03

Write first_unique_char(s) that returns the index of the first character that appears only once, or -1.

Explain it without notes

01

Find the k most frequent words in a text, with ties broken alphabetically. State the time and space complexity.

02

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?

03

What is a stable sort, and how do you sort by one field descending and another ascending?

04

How does bisect_left differ from bisect_right? Give an example with duplicates.

05

How would you find the maximum of every window of size k in an array in O(n)?

06

Given a list of numbers and a target, return the indexes of two numbers that add up to the target, in O(n) time.

07

What goes wrong when you remove items from a list while looping over it, and what do you do instead?

Counting comparisons: list scan versus set lookup
Sign in to run this example in your browser.

Expected output

n=10    list: 10    set: 1
n=100   list: 100   set: 1
n=1000  list: 1000  set: 1
same result: True