Problem · Hash Table

Assign Locker

Learn this problem
EasyGoogleINTERNOA
See Google hiring insights

Problem statement

An automatic locker system is introduced into the changing room. When a customer visits the changing room, the system works as follows:

  • If a customer has not been assigned a locker, the system assigns them the lowest available locker number;
  • If a customer has already been assigned a locker, the system opens and releases that locker. Afterwards, the locker can be reassigned to another customer.
  • The locker numbers start from 1. At the beginning of the day, all lockers are empty. The changing room is visited N times. Which locker is assigned to a customer for the last time?

    ⊹ ࣪ Credit to 𓆝⋆。˚ 77﹏𓊝﹏𓂁﹏⊹ ࣪ ˖

    Function

    assignLocker(clients: String[]) → int

    Examples

    Example 1

    clients = ["Alice", "Eve", "Bob", "Eve", "Carl", "Alice"]return = 2
    - Locker 1 is assigned to Alice; - Locker 2 is assigned to Eve; - Locker 3 is assigned to Bob; - Eve releases locker 2; - Locker 2 is assigned to Carl; - Alice releases locker 1. The last assigned locker is locker 2, so the function should return 2.

    More Google problems

    drafts saved locally
    public int assignLocker(String[] clients) {
      // write your code here
    }
    
    clients["Alice", "Eve", "Bob", "Eve", "Carl", "Alice"]
    expected2
    checking account