Skip to content
Interview prep ยท questions

CS fundamentals interview questions

8 questions, from core to senior. Say your answer out loud first, then open the model answer and check it against the points a strong answer covers.

How does a hash table work, and when is it slow?

  • Core
Show the model answer

A hash table stores entries in an array of buckets. To find a key, it runs a hash function on the key and turns the result into an index in that array. So it goes straight to one bucket instead of searching everything. Two different keys can land in the same bucket. That is a collision. One fix is chaining, where each bucket holds a small list. Another is open addressing, where the table tries the next free slot. The load factor is the number of entries divided by the number of buckets. When it gets too high, the table makes a bigger array, often twice the size, and moves every entry. That one step is slow, but it is rare, so insert is still O(1) on average. The worst case is O(n), when many keys land in the same bucket. A weak hash or an attacker who picks keys causes it, so many languages use a random seed in their hash.

A strong answer covers

  • Explains the hash function that maps a key to a bucket index
  • Names collisions and a fix: chaining or open addressing
  • Explains resizing when the load factor grows, with amortized O(1) insert
  • Says average O(1), worst case O(n) when keys collide
  • Knows that chosen keys can force collisions, and a random seed helps

Likely follow-up

Why must a key's hash not change while the key is in the table?

Why do databases use B-trees for indexes, and when is an LSM tree a better choice?

  • Senior
Show the model answer

Storage is read in pages, not single bytes, so the goal is to read as few pages as possible. A B-tree node fills a whole page and holds hundreds of keys, so the tree is very wide and very shallow. Even a huge table needs only a few page reads to find a key, and the top levels usually stay in memory. In a B+ tree all values live in the leaves, and the leaves are linked in order, so range scans are fast. The cost is on writes. Each write changes a page in place, and those are scattered random writes. An LSM tree is built for heavy writes. A write goes to a log and to a sorted table in memory. When that table is full, it is written to disk as a new sorted file, in one sequential write. In the background, compaction merges the files. Reads may check several files, so they cost more, and filters help skip files.

A strong answer covers

  • Says B-trees are wide and shallow, so a lookup reads few pages
  • Says a B+ tree keeps values in linked leaves, which makes range scans fast
  • Explains that B-tree writes are random updates in place
  • Describes the LSM tree: memory table, sorted files on disk, compaction
  • States the trade-off: LSM writes are cheap, reads may check several files

Likely follow-up

What is write amplification, and how does compaction cause it in an LSM tree?

What is the Big-O cost of common operations on an array, a linked list, a hash map, a heap and a balanced tree?

  • Core
Show the model answer

An array reads any index in O(1), and adding at the end is O(1) on average, because it sometimes grows. Inserting or deleting in the middle is O(n), because the items after it move, and searching an unsorted array is O(n). A linked list inserts or removes in O(1) when I already have the node, but reaching the k-th item is O(n). A hash map gets, sets and deletes in O(1) on average and O(n) in the worst case, and it keeps no order. A binary heap shows the smallest item in O(1), and push and pop take O(log n). Building a heap from n items takes O(n). A balanced search tree finds, inserts and deletes in O(log n), and it keeps the keys sorted, so it can answer range queries and find the next larger key. Big-O hides constants. An array is often faster than a linked list in practice, because its memory is contiguous and works well with the CPU cache.

A strong answer covers

  • Gives arrays O(1) index access and O(n) insert in the middle
  • Gives linked lists O(1) insert at a known node and O(n) access
  • Gives hash maps O(1) on average and O(n) in the worst case
  • Gives heaps O(1) peek and O(log n) push and pop, balanced trees O(log n) with order
  • Says Big-O hides constants such as cache effects

Likely follow-up

When would you use a balanced tree instead of a hash map, even though it is slower?

What happens on the call stack during recursion, why can it overflow, and how do you avoid it?

  • Core
Show the model answer

Each function call pushes a frame onto the call stack. The frame holds the arguments, the local variables and where to return. The frame stays there until the call returns. In recursion, a function calls itself before it returns, so the frames pile up, one for each level of depth. The stack has a fixed size, so very deep recursion runs out of space. In JavaScript that gives a RangeError about the maximum call stack size. Python has a recursion limit, 1000 by default, and raises RecursionError. A missing or wrong base case causes endless recursion, but correct code can overflow too, for example a depth-first search on a long linked chain. To avoid it, I turn the recursion into a loop with my own stack, an array on the heap that can grow much bigger. I push what the recursive call would get and pop it in the loop. Most JavaScript engines do not optimize tail calls, so I do not rely on that.

A strong answer covers

  • Says each call pushes a frame with arguments, locals and a return address
  • Explains that depth, not total calls, fills the stack
  • Names the errors: RangeError in JavaScript, RecursionError in Python
  • Rewrites recursion as a loop with an explicit stack
  • Does not rely on tail call optimization in JavaScript or Python

Likely follow-up

How do you turn a recursive in-order traversal of a binary tree into a loop?

Why does this code print false, and how should you store money?

  • Core
console.log(0.1 + 0.2);         // 0.30000000000000004
console.log(0.1 + 0.2 === 0.3); // false

// Compare with a tolerance instead
const close = (a, b) => Math.abs(a - b) < 1e-9;
console.log(close(0.1 + 0.2, 0.3)); // true

// Money: whole cents in integers
const priceCents = 1999;
console.log(priceCents * 3); // 5997, exact
Show the model answer

JavaScript numbers are 64-bit binary floating point numbers, the IEEE 754 double format. Binary can store halves, quarters and eighths exactly, but not tenths. So 0.1 is stored as the nearest binary value, which is a tiny bit off, and so is 0.2. When I add them, the small errors add up, and the result is not the same double as 0.3. This is not a JavaScript bug. Python, Java and C give the same result. So I never compare floats with equals. I check that the difference is below a small tolerance that fits the size of the values. For money I do not use floats at all. I store whole cents, or the smallest unit of the currency, as integers, and I format them only for display. Integers are exact in a double up to 2 to the power of 53. In the database I use a decimal or integer column.

A strong answer covers

  • Says numbers are binary IEEE 754 doubles and 0.1 has no exact binary form
  • Says the same happens in other languages, it is not a JavaScript bug
  • Compares floats with a tolerance, not with equals
  • Stores money as integer cents or a decimal type
  • Knows integers are exact only up to 2 to the power of 53

Likely follow-up

You must split 100 dollars between three people. How do you handle the leftover cent?

This code withdraws twice from a balance of 100. Why is the result wrong, and how do you fix this kind of bug?

  • Senior
let balance = 100;

async function withdraw(amount) {
  const current = balance;       // read
  await saveAuditLog(amount);    // the other call runs here
  balance = current - amount;    // write, from an old value
}

await Promise.all([withdraw(30), withdraw(50)]);
console.log(balance); // 50 or 70, never 20

The model answer to this senior question opens with a year in the app. What a year includes

What is the difference between the stack and the heap, and how does garbage collection work?

  • Core
Show the model answer

The stack holds the frames of the functions that are running: their arguments, local values and return addresses. Each thread has its own stack. Memory on it is freed automatically when a function returns, so it is very fast, but its size is limited. The heap holds data that must outlive a single call or whose size is not known in advance, such as objects, arrays and strings. In C the programmer frees heap memory by hand. In JavaScript, Java or Go a garbage collector does it. It starts from roots, such as globals and the variables on the stacks, follows every reference, and marks what it can reach. Everything else is garbage and gets freed. Most collectors are generational, because most objects die young, so they check new objects often and old ones rarely. CPython mainly uses reference counting, with a cycle collector. A leak can still happen: anything I keep a reference to, such as a cache or a listener, is never freed.

A strong answer covers

  • Says the stack holds call frames, is per thread, fast and limited in size
  • Says the heap holds data that outlives a call or has an unknown size
  • Explains reachability: from roots, mark what is reachable, free the rest
  • Mentions generational collection because most objects die young
  • Says a kept reference, such as a cache or listener, still leaks memory

Likely follow-up

How would you find a memory leak in a Node service that grows over a few days?

Why does length give these numbers, and what is the difference between code points, UTF-16 units and bytes?

  • Senior
const s = "๐Ÿ˜€";
s.length;                           // 2 (UTF-16 code units)
[...s].length;                      // 1 (code points)
new TextEncoder().encode(s).length; // 4 (UTF-8 bytes)

const thumb = "๐Ÿ‘๐Ÿฝ";
[...thumb].length;                  // 2 (thumb + skin tone)
const seg = new Intl.Segmenter("en", { granularity: "grapheme" });
[...seg.segment(thumb)].length;     // 1 (what the user sees)

The model answer to this senior question opens with a year in the app. What a year includes