FastPrepMutual Wishlist Rankings

Mutual Wishlist Rankings

Stripe logoStripe● MediumFULLTIMENEW GRADINTERNPHONE SCREEN
Learn

Problem statement

Users of an apartment-exchange service keep ordered wishlists of other users' apartments.

Each wishlist row is user:choice1,choice2,..., with rank 0 as the first choice. Process two kinds of operations:

  • MUTUAL user rank: return true when the apartment at that rank belongs to another user who also places user at the same rank.
  • CHANGED user rank: rank is at least 1. Imagine moving that entry up one position by swapping ranks rank and rank-1. Without mutating the stored wishlist, return the users whose mutual-ranked status with user would change.

Return one string per operation. A changed list is formatted as sorted usernames inside brackets, for example [a,c] or [].

Function

evaluateWishlist(wishlists: String[], operations: String[]) → String[]

Examples

Example 1

wishlists = ["a:c,d","b:d,a,c","c:a,b","d:c,a,b"]operations = ["MUTUAL a 0","MUTUAL b 0","MUTUAL a 1","CHANGED d 1","CHANGED b 2","CHANGED b 1"]return = ["true","false","true","[a]","[c]","[]"]

a/c are mutual first choices, a/d are mutual second choices, and only the source-described pairings change under each hypothetical rank bump.

Example 2

wishlists = ["amy:bo,cy","bo:cy,amy","cy:amy,bo"]operations = ["MUTUAL amy 0","MUTUAL amy 1","CHANGED amy 1"]return = ["false","false","[bo,cy]"]

Neither original rank is mutual. Swapping amy's choices creates mutual rank 0 with cy and mutual rank 1 with bo.

Constraints

  • 1 <= wishlists.length <= 1000.
  • Usernames are unique, and every listed choice names another supplied user at most once.
  • 1 <= operations.length <= 10000.
  • A MUTUAL rank may be out of range and then returns false.
  • A CHANGED rank is valid and at least 1.
  • Operations do not mutate the wishlists.

More Stripe problems

See Stripe hiring insights
public String[] evaluateWishlist(String[] wishlists, String[] operations) {
  // write your code here
}
wishlists["a:c,d","b:d,a,c","c:a,b","d:c,a,b"]
operations["MUTUAL a 0","MUTUAL b 0","MUTUAL a 1","CHANGED d 1","CHANGED b 2","CHANGED b 1"]
expected["true", "false", "true", "[a]", "[c]", "[]"]
Checking account…