Too Good To Go: give variable surplus food a clear pickup arrangement
Too Good To Go connects stores with unsold food to consumers who can collect it in person. People choose a nearby Surprise Bag and collect it during the store’s stated window, giving that day’s surplus a destination.
Break down the problem and try your own design and implementation first. Then explore the reasoning to check states, edge cases, trade-offs, and ways to test your approach. The guide offers a reasoning path, not a complete implementation.
Encapsulate pickup timing in a module you can check
Suppose an order page must repeatedly show “not yet,” “ready to collect,” or “window ended.” Put this judgment in one module so pages do not duplicate the rules and boundary times can be checked locally.
- Create the module: Supply startMs and endMs as UTC timestamps in milliseconds, plus a now function that returns the current timestamp when called. Start and end must be finite numbers with startMs < endMs; otherwise creation fails and returns invalid-window. now is guaranteed to return a finite number.
- Query status: Provide getStatus(), which reads now() on every call. Before startMs, return upcoming; from startMs up to but excluding endMs, return open; at or after endMs, return closed. Return only the status. Do not format dates or handle payment or redemption.
- Check: With startMs=1000 and endMs=2000, adjust a test clock to 999, 1000, 1999 and 2000. The same module must return upcoming, open, open and closed respectively. A window from 1000 to 1000 must be rejected at creation.
- Deliver: Submit the module, interface explanation and these local examples. Briefly explain how the clock is separated from window rules.
Explore the reasoning
-
Separate window parameters from the current time: parameters belong to the window, while the clock supplies a fresh value at query time.
-
Decide which endpoints are included, then check 1000 and 2000. Do not rely on real waiting to reproduce them.
-
Check that invalid windows are rejected at creation, and explain how the page can choose wording from the returned status.