FastPrepReturn Records 🥝

Return Records 🥝

Amazon logoAmazon● EasyINTERNOA

Problem statement

Simulate a website authentication service. Initially, no users are registered and no user is logged in. Process each request in attempts from left to right.

Each request has one of these forms:

  • register username password: register a new username. Return "Registered Successfully", or "Register Unsuccessfully" if the username already exists.
  • login username password: log in a registered user when the password matches and the user is not already logged in. Return "Logged In Successfully" on success, or "Login Unsuccessfully" otherwise.
  • logout username: log out a currently logged-in user. Return "Logged Out Successfully" on success, or "Logout Unsuccessfully" otherwise.

A failed login does not change an existing session. Usernames and passwords are case-sensitive.

Return one result string for every request, in the same order.

Function

returnRecords(attempts: String[]) → String[]

Examples

Example 1

attempts = ["register user05 qwerty", "login user05 qwerty", "logout user05"]return = ["Registered Successfully", "Logged In Successfully", "Logged Out Successfully"]

The user is registered, then logs in with the matching password, and finally logs out. All three requests succeed.

Example 2

attempts = ["register david david123", "register adam 1Adam1", "login david david123", "login adam 1adam1", "logout david"]return = ["Registered Successfully", "Registered Successfully", "Logged In Successfully", "Login Unsuccessfully", "Logged Out Successfully"]
  1. david and adam are new usernames, so both registrations succeed.
  2. david logs in with the correct password.
  3. adam uses 1adam1, which does not match the case-sensitive password 1Adam1, so that login fails.
  4. david is logged in, so the logout succeeds.

Constraints

  • 1 <= attempts.length <= 100000
  • 1 <= username.length, password.length <= 10
  • Usernames and passwords contain only digits and English letters.
  • Every request has one of the three documented formats.

More Amazon problems

See Amazon hiring insights
Saved
public String[] returnRecords(String[] attempts) {
  // write your code here
}
attempts["register user05 qwerty", "login user05 qwerty", "logout user05"]
expected["Registered Successfully", "Logged In Successfully", "Logged Out Successfully"]
Checking account…