Three classic interview problems on a k-th value and two linked-list rewires, in our own wording. Read the rank direction, keep two cursors apart, and reuse the same nodes.
Linked lists and k-th value questions appear in almost every coding round, because each one fits in a short exercise and exposes careless habits. Pointer code rewards discipline, and a rank question rewards reading the statement before you sort.
This set collects three classics that the launch districts do not cover yet. Each one is our own version of a well-known interview problem, so you can practise the classics without memorising someone else's wording.
Interviewers stress three things with these problems.
The first slip is the direction. A scores list sorted from low to high puts the largest at the end, so index k - 1 gives a different value from the one asked for.
Exercise · Break it: The k-th from the wrong end. A leaderboard asks for the k-th highest score, counting repeats, where k = 1 is the highest. This sorts the scores from low to high and returns the value at position k - 1. Find an input where it is wrong.
def kth_highest(scores, k):
ordered = sorted(scores)
return ordered[k - 1]Each problem gives itself away with a signal in the statement. Here are the signals of the three problems, with the move and the Academy lesson that teaches it.
| Signal in the statement | The move | Academy lesson |
|---|---|---|
| the k-th from the end of a chain, remove it | two cursors k apart, one walk, a placeholder before the head | Fast and slow pointers |
| reorder from both ends, in place | middle, reverse the second half, weave | List reversal, Merge and split lists |
| the k-th smallest or largest, one value | a heap of size k, or a three-way partition | Heaps and priority queues, Three-way partition |
The Academy lessons teach each move on its own. This set mixes them, so the skill you practise is choosing. Read the statement, find the signal, and name the move before you write a line.
The two cursors are easiest to see on a list stored in arrays: next_of[i] is the node after node i, and -1 means there is none. Move the lead cursor ahead by k - 1 nodes, then move both until the lead sits on the last node.
Exercise · Predict the output: Where does the trailing cursor stop?. The messages 10, 20, 30, 40 and 50 are chained in order, and k = 2 asks for the 2nd newest. The lead cursor starts at node k - 1, and the trailing cursor at node 0. What does this print?
values = [10, 20, 30, 40, 50]
next_of = [1, 2, 3, 4, -1]
k = 2
lead = k - 1
trail = 0
while next_of[lead] != -1:
lead = next_of[lead]
trail = next_of[trail]
print(values[trail])To find the k-th node from the end in one walk, keep two cursors a fixed gap apart and move both until the lead reaches the last node.
Reordering from both ends also works on a plain array, and it shows the one edge of the weave. Take the first post, then the last, then the second, then the second to last. When the count is odd, the middle post is left over, and it must be placed once.
Exercise · Put it in order: Weave the two ends. Arrange the lines into a function that returns the posts in the order first, last, second, second to last, and so on.
def reorder_posts(posts):
result = []
left = 0
right = len(posts) - 1
while left < right:
result.append(posts[left])
result.append(posts[right])
left += 1
right -= 1
if left == right:
result.append(posts[left])
return result
while left <= right:When the number of posts is odd, the weave leaves a middle post, and a loop that continues while the cursors are equal places it twice.
The same two-cursor idea finds the k-th node, and its edge is the start of the lead.
Exercise · Find the bug: The lead that starts one too far. This returns the value of the k-th newest message in a thread of values, where k = 1 is the newest. The lead cursor goes ahead of the trailing one, and both move to the end. Which line is wrong?
def kth_newest(values, k):
lead = k
trail = 0
while lead < len(values) - 1:
lead += 1
trail += 1
return values[trail]The first problem is easy and uses one idea: two cursors kept apart. The other two are medium. One rewires a chain from both ends, and one finds a k-th value in a list with many repeats. The minutes are time boxes for practice, close to what an interview allows.
Three of the twists are worth knowing before you start. Removing the node before the k-th newest must also work when it is the head, so start both cursors on a placeholder before the head. The weave must save both next pointers before it relinks. And the pickup times go up to 3,600 seconds, so 100,000 trips repeat values a lot, and a three-way partition keeps the equal values together.
In an interview, say the invariant out loud: "the trailing cursor is always k nodes behind the lead". It shows the interviewer your thinking, and it catches a wrong start early.
The three time boxes add up to 75 minutes. Split them into two sittings: the removal problem takes 15 minutes, and the other two take 60.
If you finish a problem early, use the rest of its box well. Explain the move aloud in one minute, as you would at the end of an interview, and say its cost in time and memory.
Checkpoint · Heaps and lists
Q1/5 The scores are 9, 2, 7 and 4, and you need the 2nd highest. What is it?
Answer: B
Sorted from high to low the scores are 9, 7, 4, 2, so the 2nd highest is 7. Sorting from low to high and taking position 1 returns 4.
Q2/5 To find the k-th node from the end in one walk, how far apart do you keep two cursors?
Answer: B
A fixed gap means the trailing cursor is always the same number of nodes behind the lead. When the lead reaches the end, the trail sits on the target.
Q3/5 Why do both cursors start on a placeholder before the head?
Answer: B
Removing a node needs the node before it. With a placeholder, removing the head is the same as removing any other node.
Q4/5 The weave takes the first post and the last post of five posts, then the second and the second to last. What is left?
Answer: B
For 1, 2, 3, 4, 5 the pairs give 1, 5, 2, 4, and post 3 is left. A loop that continues while the cursors are equal places it twice.
Q5/5 Pickup times go up to 3,600 seconds, and there are 100,000 trips. What does that tell you about the method?
Answer: B
At most 3,600 distinct values can appear in 100,000 trips, so many trips share a time. A three-way partition puts all equal values in one block.
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.