Six interview problems with answers that look like real numbers. The move: choose a unit small enough that the answer is a whole number, then search the whole numbers with an exact check.
You work on the back end of a build platform, a rail timetable and a delivery service. The six problems on this street ask for a length, a time, a speed or a median. In theory each is a real number, and a loop over real numbers has no end.
Each of them has a way out. Count in a unit small enough that the answer is a whole number, and search the whole numbers. The check stays in integers, so rounding errors never appear.
| Problem | The answer in whole units | The check at x |
|---|---|---|
| Smallest radius that covers all houses | a radius in whole units | every shop has a screen within x |
| Cut ropes to get k equal pieces | a length in hundredths | the pieces of that length number at least k |
| Smallest speed for a train timetable | a whole speed | the time, in hundredths of an hour, is within the limit |
| Time for two machines to finish | a time in hundredths of a minute | the work of both agents reaches the order |
| Fastest meeting point on a line | whole minutes | the reachable intervals share a point |
| Median of two sorted logs | a cut position, then twice the median | the left part ends no higher than the right part starts |
Read the middle column. Five answers are whole numbers in a chosen unit, and the sixth is a position. That is why the first-true loop from the Academy still applies.
Exercise · Predict the output: The finish time in hundredths. Agent A starts after 2 minutes and does 3 jobs a minute. Agent B starts at once and does 1 job a minute. Time is counted in hundredths of a minute. What does the search print for a batch of 1 job?
delay_a, rate_a, delay_b, rate_b, order = 2, 3, 0, 1, 1
lo, hi = 0, 400
out = ""
while lo < hi:
mid = (lo + hi) // 2
out += str(mid) + ","
done = rate_a * max(0, mid - delay_a * 100) + rate_b * max(0, mid - delay_b * 100)
if done >= order * 100:
hi = mid
else:
lo = mid + 1
print(out + str(lo))The move has two parts, and you make them before you write any code.
First, choose a unit small enough that the answer is a whole number. The problem usually names it: hundredths of a minute, hundredths of a metre, whole minutes. Scale every input to that unit before you compare anything.
Second, write the check in integers and search the whole numbers with the first-true loop. The first whole number that passes is the answer rounded up to the unit, so no rounding code is needed.
Every value in the check is a whole number in the chosen unit, so the check has no rounding error. It is false below lo and true from hi on, and lo is the answer when they meet.
A batch of one job with a total rate of 3 jobs a minute needs 0.3333 minutes. The first whole hundredth that passes is 34, which is that time rounded up.
Count in hundredths: at 33 the work is 0.99 of a job, at 34 it is 1.02, so the first true value is the time rounded up.
Exercise · Put it in order: The longest piece in hundredths. Arrange the lines into a search that returns the longest piece length, in hundredths of a metre, that gives at least k pieces. The rope lengths are in whole metres, and the helper pieces is given.
def longest_piece(ropes, k):
lo = 0
hi = max(ropes) * 100
while lo < hi:
mid = lo + (hi - lo + 1) // 2
if pieces(ropes, mid) >= k:
lo = mid
else:
hi = mid - 1
return lo
hi = max(ropes)The Academy lessons Real values and 2D and Search on the answer teach the search over real values and over answers. The first-true template teaches the loop. This street does not teach them again. It changes four things.
Floats are a trap, not a tool. Adding 0.01 a hundred times gives 1.0000000000000007, and 0.1 plus 0.2 gives 0.30000000000000004. A check built on such values can say no at the exact answer. Whole hundredths never drift.
The units are part of the check. A rope of 5 metres must be 500 hundredths before it is divided by a length in hundredths. With 1,000,000,000 metres, the top length is 100,000,000,000 hundredths. That needs 64 bits and 37 steps.
Geometry is intervals. Each friend can reach an interval of positions in t minutes. All of them can meet when the largest left end is at most the smallest right end. Two friends at 0 and 10 who both walk 1 metre a minute meet at minute 5, at the point 5.
A strict comparison misses this and answers 6.
A median is a position. For two sorted lists, do not search a value. Search how many items the shorter list gives to the left half. Search the shorter list, because a cut in the longer one can need a negative number of items from the other.
Return twice the median, so the answer stays whole. For lists of 300,000 items, the search takes 19 steps.
Exercise · Break it: The friends who touch. This returns the fewest whole minutes for all friends to reach one common point. A friend walks at a fixed speed in both directions. It tests whether the reachable intervals overlap. Find an input where it gives a wrong answer.
def meeting_time(positions, speeds):
lo, hi = 0, 1000000
while lo < hi:
mid = (lo + hi) // 2
left = max(p - s * mid for p, s in zip(positions, speeds))
right = min(p + s * mid for p, s in zip(positions, speeds))
if left < right:
hi = mid
else:
lo = mid + 1
return loExercise · Find the bug: Metres against hundredths. This counts the pieces of a given length, in hundredths of a metre, that the ropes give. The rope lengths are in whole metres. It is right for a length of 100 and wrong for others. Which line is wrong?
def count_pieces(ropes, length):
total = 0
for r in ropes:
total += r // length
return totalScale the rope to hundredths before dividing by a length in hundredths. Dividing 5 by 250 counts zero pieces.
Solve them in this order. The first three scale a length, a speed or a position to whole units. The next two work in hundredths of a minute or whole minutes, and the last searches a cut position.
Before you code each problem, write two lines: the unit you count in, and the check at one value x. If the check mixes two units, rewrite it before you write the search.
Give each problem about 25 minutes. Spend the first five on the two lines, and write the check as its own function.
Test the check by hand on three values: too small, exactly right, and too large. Then test one value that needs rounding, such as a third of a minute.
Four edge cases come back on almost every rung. A value touches the bound exactly, a list has one element or none, a total needs 64 bits, and a check mixes units. Write one small test for each.
Checkpoint · Real values and geometry
Q1/5 A finish time can be 3.2 minutes or 0.3333 minutes. What is the first step of the move?
Answer: B
A search over whole hundredths ends in a finite number of steps, and its check has no rounding error. The first true value is the answer rounded up to the unit.
Q2/5 A job needs 0.3333 minutes at a total rate of 3 jobs a minute. What is the first passing whole hundredth?
Answer: B
At 33 hundredths the agents finish 0.99 of a job. At 34 they finish 1.02 jobs, which is the first whole hundredth that passes.
Q3/5 Why does a check that divides 5 metres by a length of 250 hundredths fail?
Answer: B
Dividing 5 by 250 gives 0, while 500 // 250 gives 2. Both sides of a comparison must be in the same unit.
Q4/5 Two friends can reach the intervals from -5 to 5 and from 5 to 15. Can they meet?
Answer: B
They can both stand at 5. A strict comparison of the largest left end and the smallest right end would answer no at the exact minute.
Q5/5 Why do you search the shorter list when you find the median of two sorted lists?
Answer: B
With 6 items and 1 item, a cut of 5 in the longer list needs minus 1 items from the shorter one. A cut in the shorter list always has a valid partner cut.
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.