FastPrepFirst Unique Character in a Stream

First Unique Character in a Stream

Amazon logoAmazonâ—Ź MediumFULLTIMEPHONE SCREENONSITE INTERVIEW

Problem statement

Characters arrive in the order of the string stream. After each arrival, append the earliest character seen so far whose frequency in the processed prefix is exactly one.

If the current prefix has no unique character, append #. Return the concatenation of all per-arrival answers. Comparisons are case-sensitive.

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
  4. First Unique Character in a Stream (current)

Function

firstUniqueAfterEach(stream: String) → String

Examples

Example 1

stream = "aabc"return = "a#bb"

The first a is unique, then none is unique, and b remains first after c arrives.

Example 2

stream = "zz"return = "z#"

The only character stops being unique after its second occurrence.

Example 3

stream = "abc"return = "aaa"

The earliest a remains unique throughout all three prefixes.

Constraints

  • 1 ≤ stream.length ≤ 100000.
  • stream contains only ASCII letters and digits.
  • The sentinel # does not occur in stream.

More Amazon problems

See Amazon hiring insights
Saved
public String firstUniqueAfterEach(String stream) {
    // write your code here
}
Checking account…