KatInterview

Build a Layered In-Memory Work Tracker with Quotas and Deadlines

Data Structure DesignModel answer
MediumModel answerData Structure Designdesignin-memoryhash-map

Last updated

Design an in-memory work tracker in four growing stages: CRUD, name and priority queries, per-user limits with timed assignments, then completion and lateness reports.

Build a Layered In-Memory Work Tracker

You are asked to implement an in-memory work tracker as a class. The work is split into four stages. Each stage builds on the one before it, and everything written for earlier stages must keep working unchanged when later stages are added.

No exact method names or return formats are prescribed. Define a small, clean interface yourself, and state explicitly the rule you pick for every point left open (ID format, timestamps, missing items, ties, boundaries). Keep each such rule in one place in the code so it is easy to change.

Stage 1: create, modify and fetch

Support three operations on work items: create one, modify one, and fetch one.

Decide and state:

  • Which fields an item has (for instance a name and a numeric priority), and whether two items may share a name.
  • Whether IDs are generated by the system or supplied by the caller.
  • Whether a modification overwrites all fields or only those provided.
  • What is returned when the item does not exist or the input is invalid.

Stage 2: name lookup and priority listing

Add two read operations: finding items by name, and listing items ordered by priority.

Decide and state:

  • Whether name lookup is exact, prefix or substring, and whether it is case sensitive.
  • The order in which lookup results come back.
  • Whether a larger priority means more important, the sort direction, and the tie-breaker that makes the order fully deterministic.
  • Whether listing can be capped to the top N.

Stage 3: users, limits and expiring assignments

Add users, each with a quota, plus time-based assignments of items to users. An assignment has a start time and automatically stops being active after a given duration.

Decide and state:

  • Whether the quota caps simultaneously active assignments or the lifetime total.
  • Whether an expired assignment immediately frees quota.
  • Whether an item may be assigned to several users at once, and whether it may be re-assigned to the same user after expiry.
  • Whether an assignment ending exactly at the query timestamp is still active.
  • Whether every call carries a timestamp, and whether timestamps never decrease.

Stage 4: finishing work and lateness reporting

Add the ability for a user to mark an assigned item as finished, and a report of late assignments.

Decide and state:

  • Whether an assignment is late when it expired without being finished, and whether finishing after expiry is allowed.
  • Whether finishing releases the user's quota.
  • Whether the report covers one user or everyone, and its ordering.
  • What happens when someone tries to finish an item that is not actively assigned to them.

What is expected

Describe your design (data structures and the status model for assignments), give the complexity of each operation, and provide working code for all four stages. Call out the trade-offs you made, especially between simple scans and maintained indexes.

Hints

Hint 1

Start with a dictionary from item ID to a small record object. Give the record only the fields you need now, and keep the class layout so extra fields and maps can be added later without touching Stage 1 code.

Hint 2

Make every ordering a full sort key, for example (-priority, name, id). With a unique final component there is never any ambiguity, which makes the results repeatable and easy to test.

Hint 3

Model each assignment with an explicit status (active, finished, expired) and compute expiry lazily from the current timestamp rather than running a background timer. Put the 'is active at time t' test in one helper so the boundary rule lives in a single place.

Hint 4

For quotas, count only assignments that the chosen rule says consume quota, evaluated at the call's timestamp. Keep a per-user list of assignments so that count is cheap.