KatInterview

Recency-Evicting Store and Random Gift-Swap Pairing

Data Structure DesignModel answer
EasyModel answerData Structure Designlru-cachehash-mapdoubly-linked-list

Last updated

Two short coding tasks: build a fixed-size store that drops the least recently touched key in constant time, and pair up participants from a CSV so nobody draws themselves.

This exercise has two independent parts. Use any language you like, and explain your design as well as writing the code.

Part 1: Fixed-size store with recency-based eviction

Build a class that holds integer key/value pairs up to a fixed size.

  • The constructor takes a positive integer capacity.
  • get(key) returns the stored value, or -1 if the key is absent.
  • put(key, value) inserts a new pair or overwrites the value of an existing key.
  • If a new key is inserted while the store already holds capacity entries, exactly one entry must be removed first: the one whose last use is the oldest.
  • A key counts as "used" whenever a get finds it or a put writes it. After such an operation, that key is the freshest one.
  • Both get and put must run in amortized O(1) time.
  • Keys and values are integers, and there can be up to 10^5 operations.

Skeleton:

class Cache:
    def __init__(self, capacity: int):
        ...

    def get(self, key: int) -> int:
        ...

    def put(self, key: int, value: int) -> None:
        ...

Describe the data structures you chose and why they give constant-time behavior.

Part 2: Secret gift pairing from a CSV

The input is a CSV file with a header row name,email followed by one row per participant:

name,email
Alice,[email protected]
Bob,[email protected]
Charlie,[email protected]

Write a program that decides, for every participant, who they will buy a gift for. The rules are:

  1. Nobody may be assigned to themselves.
  2. Everyone gives to exactly one person.
  3. Everyone receives from exactly one person.

Write the result to a CSV, for example:

giver_name,giver_email,receiver_name,receiver_email
Alice,[email protected],Bob,[email protected]
Bob,[email protected],Charlie,[email protected]
Charlie,[email protected],Alice,[email protected]

Requirements:

  • The file fits in memory. If it contains fewer than 2 participants, report an error.
  • Names and emails are non-empty, and emails are unique.
  • The solution should be fast for up to 10^4 participants.
  • The pairing must be random. A fixed rotation of the input order is not acceptable unless you can justify that it is both random enough and free of self-assignments.
  • A suggested signature:
def assign_gifts(input_csv_path: str, output_csv_path: str) -> None:
    """Read participants from input_csv_path and write assignments to output_csv_path."""

Explain why your method guarantees rules 1 to 3.

Hints

Hint 1

For Part 1, a dictionary alone gives O(1) lookup but no cheap way to know which key is oldest. Think about what second structure supports O(1) removal from the middle and O(1) insertion at one end, and what the dictionary should store so it can reach that structure directly.

Hint 2

Dummy head and tail nodes in the linked list remove the special cases for an empty list or for touching the first and last elements. Remember that a `put` on an existing key must refresh it and must never evict anything.

Hint 3

For Part 2, rules 2 and 3 say the assignment is a permutation, and rule 1 says it has no fixed points. Consider shuffling and rejecting bad results, and estimate how often that succeeds. Alternatively, think about a construction that forms one big cycle, which cannot contain a fixed point.