Three medium problems the way an online assessment ends: booking slots that blur together, a mirrored stretch in a name, and a resale with a holding rule. A 65-minute round.
The last problems of an online assessment at a service company or mass recruiter are harder, and many candidates do not finish them. That is normal, and it is the reason the order of work matters. The grader gives marks for every hidden test that passes, so a correct but slow answer still scores on the small tests.
Four habits help at the end of a test.
Do not be discouraged if these take longer than the easy ones. They are meant to. The round below gives you three of them in a 65-minute box, and it is the last of three rounds in the set.
Exercise · Predict the output: Chained bookings. Bookings are sorted by start: 1 to 4, 6 to 9, 11 to 12 and 20 to 22. Two bookings join one block when the next starts at most 2 minutes after the block ends. How many blocks does this print?
slots = [[1, 4], [6, 9], [11, 12], [20, 22]]
buffer = 2
blocks = 1
end = slots[0][1]
for i in range(1, len(slots)):
if slots[i][0] - end <= buffer:
end = max(end, slots[i][1])
else:
blocks += 1
end = slots[i][1]
print(blocks)Each problem is a medium problem because of one extra rule. The Academy lesson gives the method, and the rule decides the details.
| Signal in the statement | The move | Academy lesson |
|---|---|---|
| bookings that overlap or nearly touch, joined into blocks | sort by start, grow the block while the gap is at most the buffer | Intervals |
| buy once, sell at least some days later | the lowest price among the days old enough, kept as the sale day moves | Greedy choices |
| the longest stretch that reads the same both ways | grow outwards from each of the 2n minus 1 centres | String basics |
The booking problem has one detail that decides most of its tests: a booking can sit completely inside the block. Its end is earlier than the block's end, so the block must keep the later of the two ends and never overwrite it.
Exercise · Find the bug: The block that shrinks. This counts the busy blocks after the bookings are sorted by start. A booking within the buffer joins the block. It works unless a booking sits inside an earlier, longer one. Which line is wrong?
def count_blocks(slots, buffer):
ordered = sorted(slots)
blocks = 1
end = ordered[0][1]
for start, finish in ordered[1:]:
if start - end <= buffer:
end = finish
else:
blocks += 1
end = finish
return blocksSort by start, then a booking joins the block while its gap is at most the buffer.
The resale problem looks like the classic best time to buy and sell, and the holding rule is the twist. A solution that tracks the lowest price so far passes the sample with a hold of 1 and fails when the hold is longer. The lowest price must come from days that are at least hold days before the sale.
Exercise · Break it: A resale that ignores the holding time. This finds the best profit of one purchase and a later sale, using the lowest price before the sale day. The app requires the sale to be at least hold days after the purchase. Find inputs where it returns too much.
def best_trade_with_hold(prices, hold):
best = 0
lowest = prices[0]
for sell in range(1, len(prices)):
lowest = min(lowest, prices[sell - 1])
best = max(best, prices[sell] - lowest)
return bestA resale that ignores the holding time reports a profit that the rule does not allow.
The mirrored stretch is the last of the three. The constraints say a name has at most 1,000 characters, so growing outwards from every centre is fast enough: about two thousand centres with a short growth each. For these limits that method is all you need.
All three are medium, and together they are the hard end of an assessment. Start with the problem you are surest of, so that its marks are safe before the harder ones.
The suggested order is the booking blocks, the resale and then the mirrored stretch. The first two reward a clear rule, and the third rewards careful indexes.
When these pass, change one rule, because later questions in a test often do.
Treat the three problems as the hard end of one test: 65 minutes in all, with a timer. A correct slow answer comes before a fast one.
Keep a note of the problem that took the longest. That is the pattern to revisit in the core round.
Checkpoint · Mass-recruiter stretch round
Q1/5 You have a correct but slow answer to the last problem and 20 minutes left. What is the best next step?
Answer: B
A slow correct answer passes the small hidden tests. Keep it as the reference while you improve it.
Q2/5 A name has at most 1,000 characters. Which search for the longest mirrored stretch is fast enough?
Answer: B
Growing from about two thousand centres is quick for 1,000 characters. Checking every stretch and reversing it is slower by a factor of the length.
Q3/5 A booking runs 2 to 4 inside a block that already runs until 10. What should the end of the block be afterwards?
Answer: B
The block keeps the later of the two ends. Taking the end of the booking would shrink the block.
Q4/5 Prices are 5, 1 and 4, and a sale must be at least 2 days after the purchase. What is the best profit?
Answer: B
The only allowed trade buys on day 0 at 5 and sells on day 2 at 4, a loss. The answer is 0, because no allowed trade makes a profit.
Q5/5 How many centres does a name of 5 characters have for mirrored stretches?
Answer: C
There are 5 letter centres for odd stretches and 4 gaps between letters for even ones, so 2 times 5 minus 1, which is 9.
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.