Five interview problems about lists that are sorted in pieces. The move: find the seam, the one place where the order breaks, and let one comparison tell you which side of it you are on.
You work on the back end of a bank, a streaming service and a monitoring service. The five problems on this street each hand you a list that is almost sorted. It is sorted in pieces, and there is one seam where the order breaks.
A rotated list is two rising pieces: 6, 7, 9, 12 and then 2, 4, 5. A mountain is one rising piece and one falling piece. Plain binary search fails on both, because a middle value no longer tells you which side to keep.
| Problem | The pieces | What one comparison at the middle tells you |
|---|---|---|
| Lowest reading in a rotated list | two rising pieces | the middle is above the right end, so the seam is to the right |
| Find a code in a rotated shard list | two rising pieces | the seam, then which piece can hold the code |
| Peak of the load curve | gains that never grow | the next gain is still above the price, or it is not |
| Find a value in a mountain profile | rising, then falling | the next value is higher, so you are on the rising side |
| Smallest with duplicates present | two rising pieces, with equal values | above or below the right end, or a tie that tells you nothing |
Read the last column. Each comparison turns "somewhere in a broken order" into a flag with one switch. That is what lets the Academy loop run again.
Exercise · Predict the output: Which middles does the search look at?. The code looks for the lowest value in a rotated list of distinct readings. It records the value at every middle, then prints them with the answer. What does it print?
readings = [6, 7, 9, 12, 2, 4, 5]
lo, hi = 0, len(readings) - 1
out = ""
while lo < hi:
mid = (lo + hi) // 2
out += str(readings[mid]) + ","
if readings[mid] > readings[hi]:
lo = mid + 1
else:
hi = mid
print(out + str(readings[lo]))The move has two parts. First, name the seam: the one place where the order breaks. In 6, 7, 9, 12, 2, 4, 5 it sits between 12 and 2. In a mountain it is the peak.
Second, compare the middle with the right end of the window, not with the target. In a rotated list of distinct values, a middle above the right end must have the seam to its right. A middle below the right end sits in the last rising piece, so the seam is at the middle or to its left.
At every step, the lowest value, or the peak, is inside the window from lo to hi. One comparison of the middle with the right end keeps half of the window that still holds it.
The first-true loop from the Academy runs unchanged. The flag is "the middle is not above the right end", and the lowest value is the first true. For a mountain, the flag is "the next value is lower", and the peak is the first true.
Compare the middle with the right end: above means the seam is to the right, below means it is at the middle or to its left.
Exercise · Put it in order: The peak of a mountain. Arrange the lines into a search that returns the position of the peak of a list that strictly rises, then strictly falls. The flag is "the next value is lower".
def peak_index(profile):
lo = 0
hi = len(profile) - 1
while lo < hi:
mid = lo + (hi - lo) // 2
if profile[mid] < profile[mid + 1]:
lo = mid + 1
else:
hi = mid
return lo
hi = mid - 1The Academy lesson Rotated arrays and peaks teaches the seam search on one list, and The first-true template teaches the loop. This street does not teach them again. It changes four things.
The question is not the seam. To find a code in a rotated list, find the seam first, then run the plain loop on the one rising piece that can hold the code. To find a value in a mountain, find the peak, then search the rising side.
On the falling side, reverse the comparison: the flag is "the value is at most the query".
The queries are slices. A slice of a rotated list that starts below its end contains no seam, so its lowest value is its first. Only a slice that starts above its end needs the search.
The flag can be a gain. In the load curve, the gains never grow. The flag is "the next gain is at most the price", and the first true is the best count of workers.
On a tie the smaller count wins, so the comparison is at most, never below. With the gains 8, 6, 4, 2, 0, -1 and a price of 0, counts 5 and 6 tie, and the answer is 5.
Equal values break the comparison. A middle equal to the right end does not say where the seam is. Drop the right end by one.
In 3, 3, 1, 3 the plain rule gives 3, because the tie sends it left. On a list of distinct values, 100,000 entries take 17 steps. On 100,000 equal entries, the search takes 99,999.
A tie at the right end hides the seam. Do not move hi to the middle: drop the right end by one.
Exercise · Break it: The tie at the right end. This returns the lowest reading of a rotated list. A reading can appear more than once. It treats a tie with the right end like a middle that is not above it. Find an input where it gives the wrong lowest value.
def lowest(readings):
lo, hi = 0, len(readings) - 1
while lo < hi:
mid = (lo + hi) // 2
if readings[mid] > readings[hi]:
lo = mid + 1
else:
hi = mid
return readings[lo]Solve them in this order. The first two work on a rotated list of distinct values, the third is a list of gains, and the last two add a mountain and equal values.
Before you code each problem, write two lines: where the seam is in a small example, and what the right end says on each side of it. If you cannot name the seam, draw the list as two pieces first.
Give each problem about 20 minutes. Spend the first three on the two lines, and say them aloud. Then run your search by hand on a list of five values, once with the seam in the middle and once at the very start.
Four edge cases come back on almost every rung. The list has one value, the rotation is 0 so the whole list rises, the seam is at the last position, and a value is repeated. Write one small test for each.
When a solution fails, print lo, mid and hi at every step of a small input. The seam must stay inside the window at every line.
Checkpoint · Rotated and bitonic
Q1/5 In the list 6, 7, 9, 12, 2, 4, 5, where is the seam?
Answer: B
A rotated list is two rising pieces. The seam is the one place where a value is followed by a lower one.
Q2/5 In a rotated list of distinct values, the middle is above the right end of the window. What does that tell you?
Answer: B
The middle lies in the first rising piece, which is above the whole last piece. The lowest value is further right.
Q3/5 You must find a given code in a rotated list. What is the plan?
Answer: B
The middle alone does not say which half holds the code, because the order breaks at the seam. Once you know the seam, each piece is plain sorted data.
Q4/5 The middle of the window is equal to its right end, and values can repeat. What do you do?
Answer: C
For 3, 3, 1, 3 the tie at the middle hides a lower value to the left. Dropping the right end by one is always safe, but a list of equal values then takes n steps.
Q5/5 In a mountain list, you search the falling side for a value. What changes from the rising side?
Answer: B
On a falling piece the values shrink as the position grows, so a flag that is false then true needs at most, not at least.
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.