Interview questions
What's the time and space complexity of your solution?" (the question at the end of every coding round)
How I'd answer — the structure: "Time is O(n log n): the sort dominates; the loop after it is O(n). Space is O(n) for the copy of the array — or O(1) extra if we're allowed to sort the input in place, not counting the sort's own stack. If we needed better time, we could use a HashSet for O(n) average time, at the cost of O(n) space."
The habits: name the dominant step, give both time and space, say which case if it isn't the worst case (e.g. "average, because of hashing"), and mention the trade-off you chose.
Combines: Ch 4, 5, 6.
Why is ArrayList.add O(1) if it sometimes copies the whole array?
How I'd answer: "It's amortized O(1). An ArrayList is backed by an array; when it's full, it allocates a new one 1.5 times bigger and copies everything, which is O(n) for that one call. But because capacity grows geometrically, those copies happen at sizes that form a geometric series, so across n adds the total copying is bounded by a constant times n. I simulated it: a million adds caused about 2.4 million element copies, 2.4 per add. If it grew by a fixed amount instead, the total would be quadratic. When I know the final size, I pre-size it to avoid copying at all."
Combines: Ch 7.
What's the complexity of this code?" (given the snippet below)
for (int i = 0; i < n; i++) {
String prefix = s.substring(0, i);
if (seen.contains(prefix)) count++; // seen is a HashSet<String>
}How I'd answer:
"The loop runs n times. In each round, substring(0, i) copies i characters, which is O(i). Then contains on a HashSet is O(1) on average in number
of lookups, but hashing a string of length i is O(i) too. So round i costs O(i), and the total is 0 + 1 + … + n, about n²/2 — O(n²) time, even though
it looks like a single loop. To make it linear, I'd avoid building every prefix — for instance with a rolling hash or a trie, depending on the
problem."
Combines: Ch 4 (Rule 7), 9. (Tries: Tries (coming soon).)
The input size is up to 100,000. How do you know whether your approach is fast enough?
How I'd answer: "I use about 10⁸ simple operations per second as a budget. For n = 10⁵, O(n²) is 10¹⁰ — around 100 seconds — so it won't pass. O(n log n) is about 1.7 million and O(n) is 10⁵, both easily fast enough. So I'd aim for sorting plus a linear scan, a hash map, two pointers or a heap. If n were up to 20, I'd consider exponential approaches like subsets; if it were 10¹⁸, I'd need something logarithmic like binary search on the answer or a formula."
Combines: Ch 10.
Why is naïve recursive Fibonacci slow, and how do you fix it?
How I'd answer: "Each call spawns two more, and the same subproblems are solved over and over — fib(3) inside fib(5) twice, fib(2) three times — so the call count grows exponentially, roughly O(2ⁿ); I measured about 30 million calls for fib(35). Memoization stores each result the first time, so each value from 0 to n is computed once: O(n) time and O(n) space. Going bottom-up with just the last two values gives O(n) time and O(1) space. This is the basic idea of dynamic programming: overlapping subproblems solved once."
Combines: Ch 8.