Seven interview problems about two items that belong together. The move: write the partner as one exact key, then decide what one lookup counts.
You work on the back end of a payments app, a delivery app and an ad platform. The seven problems on this street come from all three, but they ask one kind of question. Is there a second item that belongs with this one?
What "belongs with" means changes from problem to problem. Here is each rule, and the partner it gives for an item x.
| Problem | Two items belong together when | The partner of x |
|---|---|---|
| Overlap score of two ad profiles | the same topic is in both profiles | x itself, in the other profile |
| Complement of a delivery window | two durations add up to the shift | shift - x |
| Count pairs of orders that match the total | two fares add up to the total | total - x |
| Pairs of readings a fixed distance apart | two values differ by d | x + d |
| Ad banners with the same shape | width and height have the same ratio | the ratio in lowest terms |
| Smallest pair sum above a threshold | two weights add up to more than a limit | no single value |
| Pairs that differ by at most the tolerance | two readings are close in value and in time | a bucket and its neighbours |
Read the last column. When the partner is one exact value, a map finds it in one step. When it is not, as in the last two rows, you need a different tool, and this lesson shows which.
The questions also differ. Some ask for one pair, some for the number of pairs, and some for the items that have a partner at all. The same map answers each one, but it stores something different.
Exercise · Predict the output: How many pairs make the total?. The map counts the fares read so far. At each fare, the code first adds the count of its partner, then stores the fare. What does it print?
orders = [2, 3, 3, 2, 3]
total = 5
counts = {}
pairs = 0
for fare in orders:
pairs += counts.get(total - fare, 0)
counts[fare] = counts.get(fare, 0) + 1
print(pairs)The move has two parts, and you make them before you write any code.
First, write the partner of x as one exact key. For a total it is total - x, and for a gap it is x + d. For a shape, it is the width and height divided by their greatest common divisor, so 1200 by 300 and 800 by 200 both become 4 by 1. For "close", it is the bucket number x // (tol + 1), which you will meet on the last rung.
Second, decide what the map stores, because that decides what one lookup tells you.
| The question | The map stores | One lookup at item j gives |
|---|---|---|
| Find one pair | key to its first position | the partner's position |
| Count every pair | key to the count of earlier items | the pairs that end at j |
| Count pairs of distinct values | value to its count, then a loop over distinct values | one pair for each value |
| Does each item have a partner? | value to its count over all items | yes or no, with two copies needed when the partner is x itself |
Before item j is read, the map holds every earlier item under its key. So one lookup of the partner key of item j counts each pair exactly once, at its later item.
In the prediction above, the count went 0, 1, 2, 4, 6 for the fares 2, 3, 3, 2, 3. Each step added the number of earlier partners, and no pair was counted twice.
Count pairs at the later item: add the count of the partner, then store the fare, so each pair counts once.
Exercise · Find the bug: Every value is its own partner. This counts pairs of distinct values that are exactly d apart. It is right for every gap above zero, but wrong when d is 0. Which line is wrong?
def count_pairs_apart(values, d):
counts = {}
for v in values:
counts[v] = counts.get(v, 0) + 1
pairs = 0
for v in counts:
if v + d in counts:
pairs += 1
return pairsThe Academy lesson Seen-set and complement finds one pair with an exact total, in one pass. Counting and grouping counts items under a computed key, and Opposite ends moves two pointers on a sorted list. This street does not teach those again. It changes three things about them.
You count instead of find. The map now holds counts, and the answer is a sum of counts. That sum grows fast. A list of 100,000 equal fares, each half the total, holds 4,999,950,000 pairs, which does not fit in a 32-bit int.
The key is computed. A ratio becomes a pair in lowest terms: 300 by 250 and 600 by 500 both reduce to 6 by 5. Close readings become bucket numbers.
With a tolerance of 3 the buckets are 4 wide, so the readings 10, 13, 14 and 17 fall in buckets 2, 3, 3 and 4. Two readings in one bucket are always close, and a neighbouring bucket needs one more check.
Sometimes there is no key. "A sum above 100" has no single partner value. Sort the values instead, and move two pointers inward, as in the Academy's pointer lessons.
One edge case runs through the whole street: an item can be its own partner. A window of 5 minutes with a shift of 10 needs a second window of 5 minutes, not itself.
A window of half the shift finds itself in a set. Count the copies and ask for two when the partner is the item itself.
Exercise · Break it: The window that partners itself. This counts the delivery windows that have a partner: another window whose duration adds up to the shift with it. It keeps the durations in a set. Find an input where it gives the wrong count.
def windows_with_partner(durations, shift):
seen = set(durations)
count = 0
for x in durations:
if shift - x in seen:
count += 1
return countSolve them in this order. The first three use a plain key, the next two a key you compute, and the last two show what to do when no exact key exists.
Before you code each problem, write two lines: the partner of x, and what the map stores. If you cannot write the first line as one value, the problem belongs to the last two rungs.
Give each problem about 20 minutes. Spend the first three minutes on the two lines, and say them out loud as you would to an interviewer. Then list the edge cases before you run anything.
Three edge cases come back on almost every rung. An item can be its own partner, a gap or a difference can be zero, and a count of pairs can pass two billion. Write one small test for each of them.
When a solution fails, check the two lines before the code. Most wrong answers on this street come from a key that is missing a field, or from a map that stores positions when the question needs counts.
Checkpoint · Pairs and complements
Q1/5 Two readings belong together when they differ by exactly d. What is the partner key of a reading x?
Answer: B
A pair named by its smaller value v needs v + d to be present. That is one exact value, so one map lookup answers it.
Q2/5 You must count every pair i < j whose fares add up to the total. What should the map hold when fare j is read?
Answer: B
Adding the count of total - x at fare j counts every pair that ends at j. Fare j joins the map afterwards, so it never pairs with itself.
Q3/5 The shift is 10 and only one delivery window lasts 5 minutes. Does that window have a partner?
Answer: B
A pair needs two different windows. When the partner value equals the item, count the copies and ask for at least two.
Q4/5 A list holds 100,000 equal fares, and every pair of them makes the total. Why must the count be a 64-bit number?
Answer: B
The number of pairs is n × (n - 1) / 2, which is 4,999,950,000 for 100,000 fares. A 32-bit int holds about 2.1 billion.
Q5/5 Which rule has no single partner value, so a map lookup cannot answer it?
Answer: C
"More than a limit" is a range of values, not one value. Sort the weights and move two pointers inward instead.
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.