← Questions

Mutual top choices on apartment-swap wishlists

0
StripeCodingLast asked Sep 2021

A coding interview question asked in Stripe's technical phone screen, last asked Sep 2021. It tests hash map, arrays and simulation.

On an apartment-swap site, each user keeps an ordered wishlist of other users' apartments; rank 0 is their first choice. Answer two kinds of queries. Neither changes the stored wishlists.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • process(wishlists: list[str], operations: list[str]) -> list[str] — wishlist rows are user:choice1,choice2,.... One result string per operation:
    • MUTUAL user rank — "true" when the user at that rank of user's list also has user at the same rank of their own list; "false" otherwise, including when the rank is out of range.
    • CHANGED user rank (rank >= 1) — imagine swapping the entries at rank and rank - 1 in user's list. Return the users whose mutual status with user would change, sorted, as [a,c] or [].

Example

wishlists = ["a:c,d", "b:d,a,c", "c:a,b", "d:c,a,b"]

MUTUAL a 0     ->  "true"     # a's rank 0 is c; c's rank 0 is a
MUTUAL b 0     ->  "false"    # b's rank 0 is d; d's rank 0 is c
MUTUAL a 1     ->  "true"     # a's rank 1 is d; d's rank 1 is a
CHANGED d 1    ->  "[a]"      # d's list becomes a,c: a stops being mutual at rank 1
CHANGED b 2    ->  "[c]"      # b's list becomes d,c,a: c becomes mutual at rank 1
CHANGED b 1    ->  "[]"

wishlists = ["amy:bo,cy", "bo:cy,amy", "cy:amy,bo"]
CHANGED amy 1  ->  "[bo,cy]"  # amy's list becomes cy,bo: both become mutual

Constraints

  • Up to 1,000 users and 10,000 operations. Each choice names another user at most once.

Clarifying questions

  • Is "mutual" only at the same rank, or any time both users list each other?
  • Can a user's list be shorter than another's rank? (Then that rank is not mutual.)
  • Should CHANGED include users whose status goes from mutual to not mutual, as well as the reverse? (Both.)

What a strong answer covers

  • Each wishlist stored as a list plus a map from choice to rank, so "where does v rank u" is O(1).
  • MUTUAL in O(1).
  • CHANGED: only the two users at rank and rank - 1 can change, so check each before and after the swap; O(1) per query instead of recomputing all pairs.
  • Returning a sorted, de-duplicated list in the exact format.
  • Tests: out-of-range ranks, swaps that create and break mutual pairs at the same time.

Follow-ups

  • Apply the swap for real and keep answering queries.
  • Find every mutual pair at rank 0 across all users.
  • Suggest the single swap in a user's list that creates the most new mutual pairs.

Where it's asked

  • Stripe · Technical phone screen · Last asked Sep 202160 min · One realistic task in parts, in a project editor (CoderPad project mode or HackerRank Projects) with several files and tests

What it tests

hash map · arrays · simulation

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding