KatInterview

Build a Task Registry and Content-Based Duplicate Finder

Data Structure DesignModel answer
MediumModel answerData Structure Designhash-mapssorted-containersfilesystem

Last updated

Design an in-memory task manager with per-user quotas, priority ordering and audit trails, then detect duplicate files using size filtering and streaming hashes.

Context

This is a two-part screen that mixes data-structure design with a practical filesystem task. You will implement two small components in Python. Be explicit about the invariants you maintain and about the assumptions you make where the spec is silent — state them out loud before you start coding.

Part A: Task Registry

Write a TaskManager class that manages tasks belonging to many users. Every task carries:

  • id — a unique integer assigned by the manager
  • user_id — the owner
  • priority — an integer from 1 to 10
  • status — either 'pending' or 'completed'

Required methods:

  1. add_task(user_id, priority) — create a task with a fresh unique id and store it. A user may hold at most 5 tasks that are not yet completed; a sixth attempt must be rejected with an error. Priorities outside 1–10 must be rejected.
  2. list_tasks(user_id) — return that user's tasks ordered by priority, highest first. Ties must be broken deterministically.
  3. get_history(task_id) — return the chronological audit trail of status transitions for one task, including the initial creation event.

You may add whatever supporting methods the design needs (for example, something that marks a task complete) — just explain why they are necessary.

Part B: Duplicate File Detection

Given a root directory path, return groups of files whose contents are byte-identical. You have a helper compute_hash(file_path) that streams a file and returns a string digest.

Example:

tm = TaskManager()
tm.add_task("u1", 5)
tm.list_tasks("u1")          # tasks for u1, priority descending

# Input:  "/root_dir"
# Output: [["/root_dir/a.txt", "/root_dir/sub/b.txt"], ["/root_dir/c.png"]]

In the example the third group contains a single file. Treat the "list every group" behavior and the "only groups with 2+ files" behavior as a switchable option, and say which one you consider the default.

Constraints

  • Registry operations should scale logarithmically or better in the number of tasks.
  • The duplicate scan must be careful with memory: never load a whole file into RAM, and avoid hashing files you do not need to.
  • Handle unreadable entries, symlinks, and files disappearing mid-scan without crashing.

Hints

Hint 1

The registry spec never says how a task becomes completed — add that method yourself and make the quota counter move on exactly that transition, and only once.

Hint 2

To get priority-descending order with stable ties, store tuples of the form (-priority, id) in a sorted list rather than re-sorting on every read.

Hint 3

Two files with different byte sizes can never be identical, so group by size with a single stat call per file and hash only the buckets that contain two or more paths.