Library

Practical coding under time pressure

Code graded like production code. A spec that grows while you build it, concurrency done right, and the dynamic programming that still shows up.

Practice questions

15
The four-level online coding test
  1. 1.Level 3 adds timestamps to every operation, and your level 1 stored one bare value per key. What's the best first move?
  2. 2.A record is written at 40 with a TTL of 20. Which check keeps it alive for exactly the 20 seconds it was given?
  3. 3.At 10 you take a snapshot. Task a has no deadline, b is due at 10 and c at 12, and nothing has deleted expired tasks. How many pending tasks should the snapshot report?
  4. 4.A timer ends at 130. It's saved at 110 and restored at 500, and the spec says a restored timer keeps the time it had left. When does it end now?
  5. 5.Snapshots exist at 2, 4 and 6, and a restore asks for the state as of 5. Your loop returns the first snapshot whose time is at or before 5. What does it return, and what's right?
A multithreaded web crawler
  1. 1.Each fetch on the 41-page site waits about 58 ms. One page at a time took 2.36 s, and an 8-thread pool took 0.35 s. Why doesn't the GIL stop that?
  2. 2.You start a threading.Thread for every new link instead of using a pool. On the test's 41-page site with max_workers=4, how many fetches were in flight at the peak?
  3. 3.A page links to http://example.com/b#top. What should go into seen and get fetched?
  4. 4.The crawl starts at http://example.com/. Which check keeps it on that host?
  5. 5.One page's fetch raises ConnectionError, and your coordinator calls future.result() with no try. What happens?
Coin change with dollar amounts
  1. 1.What does int(0.29 * 100) return?
  2. 2.Coins of 10 and 25 cents, amount 30 cents. What does greedy return?
  3. 3.You write count_ways with the amount loop outside and the coin loop inside. What does it return for 10 cents from coins of 1, 5 and 10?
  4. 4.The amount is 500 dollars and there are 6 coin values. About how many inner-loop steps does the bottom-up min_coins take?
  5. 5.You memoize the recursion with functools.cache and try the 1-cent coin first. What happens on the 500-dollar test with Python's default settings?