~/problems / Arrays & hashing / Hash maps and counting

Account registration deduplication

easy ~15 min Airbnb

A rental site receives sign-ups one after another. Sign-up i is described by three parallel lists: names[i], emails[i] and phones[i]. A sign-up is accepted only when its email and its phone are both still unclaimed; accepting it claims both. A rejected sign-up claims nothing, so it can never block a later one.

The name is kept for the record but plays no part in the decision.

Contact details are typed by hand, so compare them in a normalised form:

  • Email: strip leading and trailing whitespace and ignore letter case. " [email protected]" and "[email protected]" are the same email.
  • Phone: keep only the digits 0-9; every other character (spaces, +, -, parentheses, dots) is ignored. "+1 (415) 555-0100" and "14155550100" are the same phone.

Write register(names: list[str], emails: list[str], phones: list[str]) -> list[bool] that returns, for each sign-up in order, True if it was accepted and False if it was rejected.

names  = ["kofi", "lena", "kofi", "omar", "pia"]
emails = ["[email protected]", "[email protected]", "[email protected] ", "[email protected]", "[email protected]"]
phones = ["555-0101", "555-0102", "555 0199", "(555) 0102", "5550199"]
register(names, emails, phones)
# [True, True, False, False, True]
# 2: email clashes with sign-up 0 (case and spaces don't matter)
# 3: phone clashes with sign-up 1
# 4: phone matches sign-up 2, but sign-up 2 was rejected, so nothing was claimed

Constraints: up to 200,000 sign-ups; the three lists have the same length (possibly 0). Every normalised email and phone is non-empty. The same person may sign up with the same name more than once; that alone is not a clash.

Show hint

Two sets of normalised values, updated only when a sign-up is accepted.

Topic: Hash maps and counting. Counter/defaultdict, complement lookups, prefix-sum + hash map.

Read the visual guide
0:00
Ctrl ' run · Ctrl ↵ submit
esc