Dsa
Trie
Medium
Usernames Directory
You need to implement a directory for usernames where users can register with unique usernames, and the system should support checking for available usernames. Create a class `UsernameDirectory` with the following methods: 1. `register(username: str)`: Registers a new username if it's available and returns 'Successfully registered' or 'Username already taken'. 2. `is_available(username: str) -> bool`: Checks if a username is available. The commands come in the format: 'REGISTER username' or 'CHECK username'. The input ends with 'END'.
Input Format:
- Each command is on a new line, and the last line contains 'END'.
Output Format:
- For each 'REGISTER' command, print the registration status. For each 'CHECK' command, print 'Available' or 'Taken'.
Example:
Input:
REGISTER alice
REGISTER bob
CHECK alice
CHECK charlie
REGISTER charlie
END
Output:
Successfully registered
Successfully registered
Taken
Available
Successfully registered
Key concepts
triestringdirectory
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now