🧮 Data Structures and Algorithms in Java — interview questions
10 questions. Filter by what the role asks for; open one for the six layers of the answer. The course is where the answers come from.
10 of 10
01A teammate says `ArrayList.add` is O(1), but you've seen it occasionally take much longer than usual. Are they wrong? What's actually happening, and when would it matter in production?Complexityeasy0-2y02An import job that works fine in testing dies with an OutOfMemoryError in production on a file with five million rows, using `List<Integer>` to hold one column. Nothing in the code looks wasteful. What's actually happening, and how would you fix it?Space complexitymedium0-2y03Someone claims `HashMap.get` is "technically O(n) worst case, so it's not really better than a list." Is that a fair characterisation of Java's HashMap? And separately, why does `ArrayDeque` beat `LinkedList` even at inserting into the front — the one thing LinkedList is supposedly built for?Collections mappingmedium3-5y04A search-as-you-type feature does `words.stream().filter(w -> w.startsWith(prefix))` over a `HashSet<String>` of two hundred thousand words, and it's slow enough on every keystroke to be noticeable. A teammate suggests switching to a trie. Is that the right fix, and is it the only option?Structures without a collectionmedium3-5y05A service models a social graph of a hundred thousand users, each with about eight connections, and someone proposes a `boolean[][]` adjacency matrix because "it's the simplest representation and edge lookup is O(1)." Walk through what that decision actually costs, and what you'd build instead.Graphshard5-8y06`Arrays.sort` on an `int[]` and on an `Integer[]` use two different algorithms under the hood. What are they, why does Java make that split, and what production bug can result from assuming they behave the same way?Sortinghard5-8y07A recursive function that walks a tree works fine in every test, then throws `StackOverflowError` in production on one particular customer's data. The recursive call is the very last statement in the method — doesn't that make it a tail call, and shouldn't the JVM optimise that away?Recursionhard5-8y08A routing feature uses BFS to find the "shortest" path between two warehouses in a graph, and it works correctly for months. Then a new requirement adds travel times to the edges, and the same BFS starts returning wrong answers on some routes while still looking like it's finding *a* path. What broke, and what's the actual fix?Graph traversalhard5-8y09A pricing engine picks the largest applicable discount tier first, then the next largest, to maximise total savings on an order — and it produces a demonstrably suboptimal answer on a specific combination of tiers, even though the same greedy approach worked correctly on every case anyone tested. How do you tell, in general, whether a problem like this can be solved greedily, and if it can't, what's the actual fix?Greedy and dynamic programmingsenior8+y10A fraud-detection service merges accounts into groups as evidence links them, and needs to answer "are these two accounts the same person?" many times a second as new evidence arrives continuously. Someone suggests running a graph traversal (BFS/DFS) on every query. What's the better structure, why, and what's the actual complexity guarantee it gives you?Union-findsenior8+y