Three interview problems that rewire chains of links. The move: save the link you are about to overwrite, so the finished part and the unfinished part are always both reachable.
You maintain the back end of an ad carousel, a ledger service and a parcel tracker. The three problems on this street come from them, and each one follows links and then rewires them.
Here is what each problem rewires, and the link you must save before you overwrite it.
| Problem | What changes | The link to save first |
|---|---|---|
| Does the carousel hop in a steady loop | nothing: you only walk the ring | the direction of the first hop |
| Sort ledger entries in a chain | the chain is cut in half and joined again | the node after the middle |
| Reverse the parcels in groups of k | the links inside each group turn round | the node after the group |
The Academy lessons taught each tool alone. Here a problem needs two of them at once, and a mistake in the order of two assignments loses part of the chain for good.
Exercise · Predict the output: Where does the left half end?. The chain is stored in an array: nxt[i] is the position after node i, and -1 ends the chain. Fast starts one node ahead of slow. What does the code print?
values = [10, 20, 30, 40, 50, 60]
nxt = [1, 2, 3, 4, 5, -1]
slow = 0
fast = nxt[0]
while fast != -1 and nxt[fast] != -1:
slow = nxt[slow]
fast = nxt[nxt[fast]]
print(values[slow])Each problem breaks into steps that change one link. A step that overwrites next without saving it cuts the chain in two, and you can no longer reach the rest.
Before you overwrite a link, keep the node it pointed to. Then the part behind you is finished and ends in a known tail, and the part ahead is still reachable.
The same rule gives three different loops.
Cut a chain at its middle: fast starts one node ahead, slow stops on the last node of the left half.
Exercise · Find the bug: The split that never shrinks. This returns the position of the last node of the left half, so that you can cut the chain after it. It works on long chains but a sort built on it never ends. Which line is wrong?
def left_end(nxt):
slow = 0
fast = 0
while fast != -1 and nxt[fast] != -1:
slow = nxt[slow]
fast = nxt[nxt[fast]]
return slowThe Academy lessons Fast and slow pointers, Reversing lists in place and Merge, split and rebuild lists each teach one tool on a plain chain. This street changes three things.
The chain is hidden in an array. The carousel stores a hop for each slot, and the next slot is (s + hops[s]) wrapped round the ring. Meeting inside a loop is no longer enough.
With hops [1, -1] the pointers meet on slot 0 in round 2, but the loop turns round, so the answer is false. A steady loop keeps one direction and has at least two slots, so a hop onto the same slot does not count.
The tools combine. Slow and fast cut the chain at its middle, and merge puts the halves together. Sorting 100,000 random entries took 1,536,476 compares, close to n log2 n, which is about 1.7 million. Comparing every pair would take 4,999,950,000.
The edge has a rule. With k = 3 and seven nodes, the groups are 1 2 3, 4 5 6 and 7. The result is 3 2 1 6 5 4 7, because the short last group stays as it is. The tail of each reversed group must also point at the head of the next one.
Reversing a short last group too gives the wrong answer: only full groups of k turn round.
Exercise · Break it: The group that is too short. This reverses a chain in groups of k. The chain is shown as an array of values in order. Find an input where it gives the wrong answer.
def reverse_groups(values, k):
out = []
for i in range(0, len(values), k):
out += values[i:i + k][::-1]
return outSolve them in this order. The ring only walks, the sort cuts and joins, and the group reversal rewires the most links.
Before you code each problem, write the link you will save. For the ring it is the direction, for the sort it is the node after the middle, and for the groups it is the node after the group.
Give each problem about 25 minutes. Draw a chain of five nodes first, and move one pointer at a time with a pencil. If a pencil step loses a node, the code will too.
Test three edge cases on every rung. Use a chain of one node, a chain of two nodes, and a chain whose length does not divide evenly. Most wrong answers here come from one assignment placed before the save.
Checkpoint · Rings, reversals and merges
Q1/5 You reverse one group of a chain. Which node must you save before rewiring the group?
Answer: B
The new tail of the group must point at the node that followed the group. Without a saved copy, the rest of the chain is lost.
Q2/5 You cut a chain of two nodes in half. Fast starts on the head, like slow. What happens?
Answer: B
Slow ends on the last node, so the left half is the whole chain and the sort never reaches its base case. Start fast one node ahead.
Q3/5 In a ring of hops [1, -1], slow and fast meet on slot 0. Is there a steady loop?
Answer: B
A steady loop keeps one direction. Here one hop goes forward and one goes back, so the loop is not steady and the answer is false.
Q4/5 Five nodes are reversed in groups of 3. What is the right result for the values 1, 2, 3, 4, 5?
Answer: B
Only full groups turn round. The last two nodes form a short group and stay as they are, so the result is 3 2 1 4 5.
Q5/5 Sorting 100,000 random entries in a chain by cutting and merging took 1,536,476 compares. What does that tell you?
Answer: A
The count is close to n log2 n, which is about 1.7 million for 100,000 entries. Comparing every pair would take 4,999,950,000.
Review cards from this lesson.
A few questions, then explain the idea back in your own words. Your tutor marks it. Quiz and explain back.