Software interviews
Data structures checklist for coding interviews, with operation costs
Most intern coding problems need about eight structures: array, string, hash table, set, stack, queue, heap and tree or graph. This checklist gives what each one is for, the cost of its main operations as the Python documentation lists them for built-in types, which structure to reach for by the signal in the problem, and three original problems with checked answers.
Your first hour with George Li (60 minutes by video, $250) ends with a written plan within one working day.
Book your first hour ($250)The structures that cover most intern problems
A structure is a way of storing items so that some operations are cheap. Choosing one is choosing which operations you want to be cheap. The course description of MIT’s Introduction to Algorithms covers the mathematical modeling of computational problems, common algorithms and the data structures used to solve them, and emphasizes the relationship between algorithms and programming. Here is the working list, each with its main use:
- Array or list: items in order, fast access by position.
- String: a sequence of characters.
- Hash table (a dict or a set in Python): fast lookup by key, and fast checks of whether you have seen a value.
- Stack: last in, first out. Matching brackets, undoing steps, evaluating expressions.
- Queue: first in, first out. Processing in arrival order, and searching a graph by layers.
- Heap: the smallest (or largest) item is always on top. Keeping the best few of many items.
- Tree and graph: items with links. A tree has one path between any two items. A graph may have many.
What each operation costs in Python
The figures below come from the Python documentation’s table of time complexity of operations on built-in types, where n is the number of items. Other languages differ in detail, but the shapes are the same. The documentation lists dict and set operations as average-case times and says that in the worst case, when every key hashes to the same value, each one takes time proportional to n.
| Structure | Operation | Cost |
|---|---|---|
| list | Read or write by position, l[k] | O(1) |
| list | Append at the end | O(1), amortized |
| list | Insert or pop at position k | O(n - k): about n at the front, O(1) at the end |
| list | Is x in the list, min, max | O(n) |
| list | Sort | O(n log n) |
| dict | Key in d, read, write or delete by key | O(1) on average, O(n) worst case |
| set | x in s, add, remove | O(1) on average, O(n) worst case |
| set | Union of two sets | O(len(s1) + len(s2)) |
| set | Intersection of two sets | O(min(len(s1), len(s2))) |
| any of them | len(x) | O(1) |
Two entries in the table are the ones that decide interview answers. A membership test on a list is a scan, so asking whether you have seen a value inside a loop makes the loop quadratic. The same test on a set or a dict is a lookup. And a list used as a queue is slow at the front: the Python tutorial says inserts or pops from the beginning of a list are slow, because every other element has to shift by one. It recommends collections.deque, which it says was designed to have fast appends and pops from both ends.
The documentation’s table does not list a heap. The costs below are derived here, and problem 2 shows the mechanism: a heap of n items is a nearly complete tree about log n levels tall, an insert adds at the end and swaps upward at most once per level, and removing the smallest swaps downward the same way.
| Structure | Operation | Cost | Why |
|---|---|---|---|
| Heap | Look at the smallest item | O(1) | It is stored at the root |
| Heap | Insert, or remove the smallest | O(log n) | One swap per level of a tree about log n levels tall |
| Stack or deque | Push or pop at one end | O(1) | Nothing else has to move |
| Binary search tree, balanced | Find, insert | O(log n) | Each step discards about half of the remaining items |
| Binary search tree, one long chain | Find, insert | O(n) | Sorted insertion order makes a chain, so there is no halving |
Which structure to reach for, by the signal in the problem
Read the problem for the operation it repeats. The structure follows from the operation.
| If the problem keeps asking | Reach for | Because |
|---|---|---|
| Have I seen this value before? | Set or dict | Membership becomes a lookup |
| How many times does each value occur, or how many pairs fit a rule? | Dict of counts, or a table grouped by a remainder | One pass instead of checking every pair |
| What matches the most recent unmatched item? | Stack | Last in, first out |
| Who has waited longest? | Queue (deque) | First in, first out, with cheap ends |
| What are the k best so far? | Heap | The worst of the kept items is on top |
| What is the nearest smaller or larger value to the left? | Stack of candidates | Each item is pushed and popped once |
| Is there a path, and how short is it? | Graph with a queue | Searching by layers finds the shortest path by edge count |
| Is the data sorted, or can it be? | Binary search or two pointers | Each step discards a large part of the input |
Three problems with checked answers
Work each on paper before opening the steps. They are original problems written for this page. The first is a stack, the second a heap and the third a hash table with collisions.
Problem 1. A postfix expression writes each operator after its two operands. Evaluate 7 3 2 * + 4 - 5 * with a stack, where each operator takes the top two values and pushes the result. Give an integer.
Show the answer and the working
Answer: 45
- Push 7, 3 and 2. The stack is 7, 3, 2.
- The operator * pops 3 and 2 and pushes 6. The stack is 7, 6. Then + pops both and pushes 13.
- Push 4. The operator - pops 13 and 4 (13 is the left operand) and pushes 9.
- Push 5. The operator * pops 9 and 5 and pushes 45. One value is left, so the expression was well formed.
Problem 2. Insert 12, 7, 15, 3, 9, 1, 8 one at a time, in that order, into an empty min-heap stored in an array with the root at position 0. Each insert adds the value at the end and swaps it with its parent while it is smaller than its parent. Give the final array as numbers separated by commas and a space.
Show the answer and the working
Answer: 1, 7, 3, 12, 9, 15, 8
- The parent of position i is position floor((i - 1) / 2). Insert 12, then 7: 7 is below 12, so they swap and the array is 7, 12.
- Insert 15 (no swap), then 3: its parent is 12, so swap, then its new parent is 7, so swap again. The array is 3, 7, 15, 12.
- Insert 9 at position 4: its parent is 7, which is smaller, so it stays. Insert 1 at position 5: its parent 15 swaps, then its parent 3 swaps. The array is 1, 7, 3, 12, 9, 15.
- Insert 8 at position 6: its parent is 3, which is smaller, so it stays. Each insert takes at most about log n swaps.
Problem 3. A hash table has 11 slots numbered 0 to 10. The hash of a key is the key mod 11. A key goes in its hash slot, or in the next free slot after it (wrapping from slot 10 to slot 0) when that slot is taken. Insert 22, 33, 15, 44, 26, 37 and 4 in that order. Counting every slot you look at, including the slot where a key finally goes, how many slots are looked at in total? Give an integer.
Show the answer and the working
Answer: 16
- Hashes: 22 and 33 and 44 all hash to 0; 15, 26, 37 and 4 all hash to 4.
- Slots looked at per key: 22 takes 1 (slot 0). 33 takes 2 (slots 0, 1). 15 takes 1 (slot 4). 44 takes 3 (slots 0, 1, 2).
- 26 takes 2 (slots 4, 5). 37 takes 3 (slots 4, 5, 6). 4 takes 4 (slots 4, 5, 6, 7).
- The total is 1 + 2 + 1 + 3 + 2 + 3 + 4 = 16. Keys that share a hash pile into one run of slots, which is why a table with many collisions slows every lookup.
How to use the checklist
- Read a problem and write down the repeated operation before you write any code.
- Name the structure and the cost of that operation in the structure you chose.
- State the cost of the whole method, and say whether a different structure would be cheaper.
- After the problem, add one line to your own copy of the cost table for anything you did not already know.
Return to the coding and complexity practice for running-time problems, and set a plan with the coding prep schedule.
Every answer on this page is recomputed by a separate program when the site is built, by running the structure or counting every case instead of following the written steps. A wrong answer would stop the build. Read how the problems are checked.
These are our own practice problems, written for this site. If you have signed an agreement about an assessment, keep its contents to yourself, including in a coaching session.
A session with George can use the time on your own problems: you work one aloud and he asks about the structure you chose. You write the code. See the packages page.
Questions
Which data structures should I know for an internship coding interview?
Array, string, hash table, set, stack, queue, heap and tree or graph cover most intern problems. Know what each one is for and what its main operations cost.
What does a set or dict lookup cost in Python?
On average it takes constant time, O(1), and in the worst case O(n), per the Python documentation. A membership test on a list is O(n).
Why is a list a poor queue in Python?
Inserting or popping at the front of a list is slow because every other element shifts by one, per the Python tutorial. It recommends collections.deque for queues.
Want help with your own applications? See internship interview coaching, a career coach for college internships, or resume review for internships.