Python interview prep

Python interview questions, from your first round to FAANG

156 questions in three levels. Write real Python in your browser, run it against visible and hidden test cases, then do what interviewers actually score: analyse the time and space complexity, measure it, optimise it and explain it — with Solvi, the AI coach, one click away.

The nine steps every question trains

Getting the right output is step three of nine. Each question page tracks these for you — several tick themselves as you run, measure and submit.

  1. 01

    Understand

    Restate it. Ask about input size and edge cases.

  2. 02

    Brute force

    Get something correct before something clever.

  3. 03

    Correct output

    Run the examples until they pass.

  4. 04

    Time complexity

    Count the work as n grows.

  5. 05

    Space complexity

    Count the extra memory.

  6. 06

    Bottleneck

    Find the line that dominates.

  7. 07

    Optimise

    Usually a set, a dict, two pointers or a heap.

  8. 08

    Explain

    Say why the optimisation works.

  9. 09

    Edge cases

    Empty, one element, duplicates, negatives.

Example — intersection of two lists. The first version checks x in list2 for every element: O(n × m). The bottleneck is that membership test. Turn list2 into a set and each lookup becomes O(1), so the whole thing is O(n + m). That “solve → analyse → optimise → explain” story is the answer. Try it →

Interactive

Feel the difference Big-O makes

Drag n. Bars are on a log scale — each grid step is 10× more work.

  • O(1)

    dict lookup, list[i], append · 1 step

    ≈ < 1 µs
  • O(log n)

    binary search, heap push · 10 steps

    ≈ < 1 µs
  • O(n)

    one loop, x in list, sum() · 1,000 steps

    ≈ 20 µs
  • O(n log n)

    sorted(), merge sort · 9,966 steps

    ≈ 199 µs
  • O(n²)

    nested loops over the input · 1.0 × 10^6 steps

    ≈ 20.0 ms
  • O(2ⁿ)

    every subset, naive recursion · > 10³⁰ steps

    ≈ longer than the universe

Times assume about 50 million simple operations per second, a rough figure for CPython. Real code has constants Big-O ignores — what it predicts reliably is how cost grows.

Python time complexity cheat sheet

Most “why is this slow?” answers in an interview come from this table. CPython, average case.

OperationCostWhy it matters
list[i], list[i] = x, len(list)O(1)Arrays of pointers — indexing is direct.
list.append(x), list.pop()O(1) amortisedOccasional resize, spread over many appends.
list.insert(0, x), list.pop(0)O(n)Every other element shifts. Use collections.deque.
x in list, list.index(x), list.count(x)O(n)A linear scan — the most common hidden O(n²).
x in set, set.add(x), dict[k], k in dictO(1) averageHash tables. Worst case O(n), almost never seen.
sorted(xs), list.sort()O(n log n)Timsort; stable; O(n) on already-sorted data.
heapq.heappush / heappopO(log n)A binary heap in a list. heapq.heapify is O(n).
deque.append / appendleft / popleftO(1)The right queue. Indexing the middle is O(n).
s + t, s[a:b], list(xs), xs[:]O(k)Copies k elements — slicing in a loop adds up.
"".join(parts)O(total length)Build strings this way, not with += in a loop.
bisect.bisect_left(sorted_xs, x)O(log n)Binary search; insort is O(n) because of the insert.
Counter(xs), set(xs), dict.fromkeys(xs)O(n)One pass, O(n) memory.

The roadmap

Work top to bottom, or jump to the topic you are weakest in. Progress is saved on this device.

0/156 solved
L1

Foundations

Freshers and first Python interview · 30 questions

Progress0/30
L2

Working engineer

2–6 years, product and data teams · 95 questions

Progress0/95
L3

FAANG

Big-tech coding rounds · 31 questions

Progress0/31

Sliding window & monotonic deque

Grow, shrink and never look back.

Binary search on answers

Halving on rotated arrays, partitions and predicates.

Data-structure design

Tries and the structures behind autocomplete.

Questions about this prep

What do L1, L2 and L3 mean?

L1 is the fresher round: loops, conditions, strings and lists. L2 is what a working engineer with a few years of experience is asked: hashing, recursion, Python internals such as decorators and generators, OOP, and practical problems like LRU caches, rate limiters, concurrency and streaming large files. L3 is the FAANG coding round: two pointers, sliding windows, binary search on rotated or partitioned arrays, heaps, graphs, trees, dynamic programming and backtracking.

Do I need to install Python?

No. Every question runs real CPython 3.13 inside your browser via WebAssembly. Your code never leaves your device, and it is saved locally as you type.

How do the test cases work?

Run executes the visible examples. Submit runs every test, including hidden edge cases such as empty input, duplicates and negatives. Each result shows the input, the expected output, your output, anything you printed and how long it took.

How do I learn time and space complexity here?

Every question has a Complexity Lab: you commit to a Big-O for time and space, see the optimal answer and why, then measure your own code, the optimal solution and the brute force at growing input sizes and watch the curves. The roadmap also has an interactive Big-O explorer and a cheat sheet of Python operation costs.

What does the Solvi AI coach do?

Solvi is the floating assistant on every question. It sees your code and your failing tests, explains the problem, gives one hint at a time, explains errors, estimates the complexity of your code and can play the interviewer for a mock explanation. It will not write the full solution for you — that is behind the Reveal button.

Are the questions free?

Yes. All questions, solutions, the Complexity Lab and the Solvi coach are free.

New to Python itself? Start with the free Python handbook, or practise data-engineering Python on /practice.