Interview Questions, Answered by Running Them
The questions actually asked about strings, lists and dictionaries - each with the code, run in your browser.
About this track
Interview answers are not recall problems. "Why is this O(n²)?" is answered properly by showing the count, and "which is faster?" by measuring both - which is what every page here does. Each question gets its own page: the answer in prose, a visualisation that steps through the mechanism, and a real Python interpreter with the implementation already in it.
The track is organised the way the questions are asked - strings, then lists and arrays, then dictionaries, hashing and the complexity traps that catch most candidates. Conceptual questions and coding problems sit side by side, because interviews mix them.
Every other track is organised around ideas. This one is organised around questions, because that is the shape the pressure arrives in: someone asks why your loop is quadratic, and you have thirty seconds. Each page is one question, answered in full and then demonstrated by code you can run.
What you will be able to do
- Answer the immutability, hashability and complexity questions without hedging.
- Reach for the right technique on sight - two pointers, sliding window, prefix sums, a counter.
- State the time and space cost of your own solution before being asked for it.
- Recognise the traps: `in` on a list, `+=` on a string, `pop(0)` as a queue.
- Show your working - the pages measure their claims, and so can you.
How the track is ordered
The track runs strings, then lists and arrays, then dictionaries, hashing and the crossover problems, finishing on the complexity traps. Within each group the conceptual questions come before the coding problems that lean on them, because "why is `in` slow on a list?" is the answer to half the coding questions that follow it. Nothing here assumes you have read the rest of the site, though the algorithms track covers the same techniques at more length.
Where this leads
The Algorithms and Data Structures track is the long-form version of this one: same techniques, one page per algorithm rather than one page per question, with the visualisation carrying more of the explanation. If a question here lands on something unfamiliar - binary search bounds, hash collisions, dynamic programming - that track has the full treatment.
All 29 modules, in teaching order
- 01Why are Python strings immutable?
- 02What does slicing a string cost?
- 03Does len() count characters or bytes?
- 04What is the difference between str and bytes?
- 05Why does `is` sometimes work on strings?
- 06find() vs index() vs `in` — which one?
- 07Reverse a string
- 08Check whether a string is a palindrome
- 09Are two strings anagrams?
- 10Group anagrams together
- 11Longest substring without repeating characters
- 12First non-repeating character
- 13Valid parentheses
- 14Run-length string compression
- 15Longest common prefix
- 16Implement substring search (strStr)
- 17Isomorphic strings
- 18What is a Python list underneath?
- 19Why does [[0]*3]*3 break?
- 20Why is `in` slow on a list but fast on a set?
- 21Two Sum
- 22Maximum subarray sum (Kadane)
- 23Remove duplicates from a sorted array in place
- 24Rotate an array by k
- 25Product of array except self
- 26How does a Python dict work?
- 27Why must dictionary keys be hashable?
- 28Count things with a dictionary
- 29What is the complexity of this code?