Interview questions
These are longer questions that combine several chapters. Try each out loud first.
Why does 0.1 + 0.2 not equal 0.3, and how would you handle money in a payments system?
How I'd answer: "Computers store fractions in binary floating point — IEEE 754. Just like 1/3 can't be written exactly in decimal, 0.1 can't be written exactly in binary. So 0.1 and 0.2 are each stored as the nearest possible value, and their tiny errors add up to 0.30000000000000004.
For money, I'd never use floats. I'd store amounts as integers in the smallest currency unit — paise or
cents — so ₹499.99 becomes 49999, or use an exact decimal type like BigDecimal in Java, Decimal in
Python, or NUMERIC in Postgres. That way additions are exact and rounding only happens where the
business rules say it should."
A user reports their name shows as José instead of José. What's going on?
How I'd answer:
"That's an encoding mismatch — mojibake. é in UTF-8 is two bytes, C3 A9. If some part of the system
reads those bytes as a one-byte-per-character encoding like Latin-1, C3 shows as à and A9 as ©.
So somewhere the data was written as UTF-8 and read as something else.
I'd check each hop: the HTTP Content-Type charset, the database and connection encoding, and any
file reading code. The fix is to use UTF-8 end to end and declare it explicitly at every boundary."
Both are O(n). Why might summing an array be much faster than summing a linked list?
How I'd answer: "Because of the CPU cache. Array elements sit next to each other in memory. When the CPU reads one, it pulls in a 64-byte cache line, so the next several elements are already in cache — that's spatial locality. Linked list nodes are scattered around memory, so each step can be a cache miss that costs around 100 nanoseconds for a RAM trip, versus about 1 nanosecond for L1. Big-O counts steps; it doesn't count how expensive each step is. The memory layout can make a several-times difference."
Combines: Ch 6 (memory hierarchy, cache lines), Ch 7 (addresses/references).
What's the output, and what's the bug?" int total = 2_000_000_000 + 2_000_000_000;
How I'd answer:
"int in Java is 32-bit two's complement, max about 2.147 billion. Four billion doesn't fit, so it
overflows silently and wraps around to a negative number: −294,967,296. Java won't warn at runtime.
The fix is to use long — written 2_000_000_000L + 2_000_000_000L so the addition itself is done in
64 bits — or Math.addExact, which throws an exception on overflow instead of silently wrapping."
Check: 4,000,000,000 − 2³² (4,294,967,296) = −294,967,296 ✔
Combines: Ch 2 (overflow), Ch 4.1 (two's complement, Java int range).
Why is a network call so much more expensive than a function call? How does that affect design?
How I'd answer: "A function call stays inside the CPU and memory — nanoseconds. A network call leaves the machine: even within one data centre a round trip is around half a millisecond, which is hundreds of thousands of times slower than reading from cache, and India to the US is around 200 milliseconds. So in design I try to cut the number of round trips: batch requests, cache results close to where they're used, and avoid chatty designs where one user request fans out into dozens of sequential calls."
Combines: Ch 6 (latency table). You'll build on this in System design (coming soon).