FastPrepFirst User to Log In Exactly Once

First User to Log In Exactly Once

Amazon logoAmazonâ—Ź EasyINTERNNEW GRADONSITE INTERVIEW

Problem statement

The array userIds records successful login events in chronological order. A user may appear more than once.

Return the ID of the earliest user in the event stream whose total number of logins is exactly one. If every user logged in more than once, return the empty string.

"Earliest" refers to the position of the user's only event in userIds, not lexicographic order.

Sibling variants: same core, different skin

These four problems are sibling variants of the same first-unique pattern. Their story wrappers and query timing differ, while the core rule keeps the earliest value whose frequency is exactly one.

  1. Longest-Waiting Unique Task
  2. Dynamic Longest-Waiting Unique Task Queries
  3. First User to Log In Exactly Once (current)
  4. First Unique Character in a Stream

Function

firstSingleLogin(userIds: String[]) → String

Examples

Example 1

userIds = ["alice","bob","alice","carol"]return = "bob"

Alice appears twice. Bob and Carol each appear once, and Bob's event comes first.

Example 2

userIds = ["a","a","b","b"]return = ""

Every user appears more than once, so the answer is empty.

Example 3

userIds = ["zoe"]return = "zoe"

The only user logged in exactly once.

Constraints

  • 1 <= userIds.length <= 100000.
  • Each user ID is a nonempty alphanumeric string of length at most 40.
  • Events appear in chronological order.

More Amazon problems

See Amazon hiring insights
Saved
public String firstSingleLogin(String[] userIds) {
  // write your code here
}
Checking account…