Quick Overview

Starting from a lowercase string, repeatedly pick a character from the current interval, append it to an identifier, and keep only the left or right remainder. Return the lexicographically largest identifier possible. Tests careful reasoning about lexicographic order and reachable choices on inputs up to 100,000 characters.

Lexicographically Largest String From Repeated Pick-and-Discard-One-Side Moves

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Online Assessment

You are given a string `key` of lowercase English letters, and you build an identifier from it by repeatedly taking one character and discarding part of the string. Positions are 1-based. Let `n` be the length of `key`. Start with the interval `[l, r] = [1, n]` and an empty identifier. While the interval is not empty (`l <= r`), make one move: 1. Choose any position `i` with `l <= i <= r`. 2. Append the character at position `i` of `key` to the identifier. 3. Keep either the left part `[l, i - 1]` or the right part `[i + 1, r]` as the new interval, and permanently discard the other part. The process ends when the interval becomes empty (`l > r`). Return the lexicographically largest identifier that can be produced. ### Function Signature ```python def largest_identifier(key: str) -> str: ``` ### Rules - Lexicographic order is the usual string order: compare the first position where two strings differ; if one string is a prefix of the other, the shorter string is smaller. - Either part may be kept even if it is empty; keeping an empty part ends the process. - The process cannot stop while the interval still contains positions. - Positions in this description are 1-based: position `i` is `key[i - 1]` in Python. - The lexicographically largest identifier is unique; return it as a string. ### Constraints - `1 <= len(key) <= 10^5` - `key` contains only lowercase English letters `a` to `z`. ### Examples **Example 1** ```text Input: key = "abc" Output: "cba" ``` Choose position 3 (`c`) and keep `[1, 2]`; choose position 2 (`b`) and keep `[1, 1]`; choose position 1 (`a`), after which the interval is empty. **Example 2** ```text Input: key = "zazbz" Output: "zzzb" ``` Choose position 1 and keep `[2, 5]`; choose position 3 and keep `[4, 5]`; choose position 5 and keep `[4, 4]`; then choose position 4 (`b`). The `a` at position 2 is discarded along the way. **Example 3** ```text Input: key = "bdacadb" Output: "ddca" ``` Choose position 2 (`d`) and keep `[3, 7]`; choose position 6 (`d`) and keep `[3, 5]`; choose position 4 (`c`) and keep `[3, 3]`; then choose position 3 (`a`). Keeping `[7, 7]` after the second `d` instead would give `ddb`, which is smaller.

Overview: Starting from a lowercase string, repeatedly pick a character from the current interval, append it to an identifier, and keep only the left or right remainder. Return the lexicographically largest identifier possible. Tests careful reasoning about lexicographic order and reachable choices on inputs up to 100,000 characters.

Read the full Amazon Software Engineer interview experience this question came from

You are given a string `key` of lowercase English letters. You build an identifier from it by repeatedly taking one character and discarding part of the string. Positions are 1-based. Let `n` be the length of `key`. Start with the interval `[l, r] = [1, n]` and an empty identifier. While the interval is not empty (`l <= r`), make one move: 1. Choose any position `i` with `l <= i <= r`. 2. Append the character at position `i` of `key` to the identifier. 3. Keep either the left part `[l, i - 1]` or the right part `[i + 1, r]` as the new interval, and permanently discard the other part. The process ends when the interval becomes empty (`l > r`). Implement `largest_identifier(key)`, which returns the lexicographically largest identifier that can be produced, as a string. **Rules** - Lexicographic order is the usual string order: compare the first position where two strings differ; if one string is a prefix of the other, the shorter string is smaller. - Either part may be kept even if it is empty; keeping an empty part ends the process. - The process cannot stop while the interval still contains positions. - Position `i` in this description is `key[i - 1]` in 0-based indexing. - The lexicographically largest identifier is unique. **Constraints** - `1 <= len(key) <= 10^5` - `key` contains only lowercase English letters `a` to `z`. **Example 1** ```text Input: key = "zazbz" Output: "zzzb" ``` Choose position 1 and keep `[2, 5]`; choose position 3 and keep `[4, 5]`; choose position 5 and keep `[4, 4]`; then choose position 4 (`b`). The `a` at position 2 is discarded along the way. **Example 2** ```text Input: key = "bdacadb" Output: "ddca" ``` Choose position 2 (`d`) and keep `[3, 7]`; choose position 6 (`d`) and keep `[3, 5]`; choose position 4 (`c`) and keep `[3, 3]`; then choose position 3 (`a`). Keeping `[7, 7]` after the second `d` instead would give `"ddb"`, which is smaller.

Constraints

  • 1 <= len(key) <= 10^5
  • key contains only lowercase English letters 'a' to 'z'.

Examples

Input: ('a',)

Expected Output: 'a'

Explanation: Minimum valid key: the single character is picked and the interval becomes empty.

Input: ('abc',)

Expected Output: 'cba'

Explanation: Source example 1, strictly increasing: every character is used, largest first.

Hints

  1. Identifiers are compared from their first character, so first ask which character can come first and how many times it can be repeated at the start.
  2. After a pick, every later pick lies on one side of it. Once several picks have been made, which stretch of the key can the remaining interval be?
  3. A longer string beats its own prefix, so compare candidate continuations in full, not only by their first letter or by their length.

Largest Identifier from a Shrinking Interval

mediumAmazonSoftware Engineer

You are given a string key of lowercase English letters. You build an identifier from it by repeatedly taking one character and discarding part of the string.

Positions are 1-based. Let n be the length of key. Start with the interval [l, r] = [1, n] and an empty identifier. While the interval is not empty (l <= r), make one move:

  1. Choose any position i with l <= i <= r.
  2. Append the character at position i of key to the identifier.
  3. Keep either the left part [l, i - 1] or the right part [i + 1, r] as the new interval, and permanently discard the other part.

The process ends when the interval becomes empty (l > r). Implement largest_identifier(key), which returns the lexicographically largest identifier that can be produced, as a string.

Rules

  • Lexicographic order is the usual string order: compare the first position where two strings differ; if one string is a prefix of the other, the shorter string is smaller.
  • Either part may be kept even if it is empty; keeping an empty part ends the process.
  • The process cannot stop while the interval still contains positions.
  • Position i in this description is key[i - 1] in 0-based indexing.
  • The lexicographically largest identifier is unique.

Constraints

  • 1 <= len(key) <= 10^5
  • key contains only lowercase English letters a to z.

Example 1

Input:  key = "zazbz"
Output: "zzzb"

Choose position 1 and keep [2, 5]; choose position 3 and keep [4, 5]; choose position 5 and keep [4, 4]; then choose position 4 (b). The a at position 2 is discarded along the way.

Example 2

Input:  key = "bdacadb"
Output: "ddca"

Choose position 2 (d) and keep [3, 7]; choose position 6 (d) and keep [3, 5]; choose position 4 (c) and keep [3, 3]; then choose position 3 (a). Keeping [7, 7] after the second d instead would give "ddb", which is smaller.

Examples

Example 1
Input
('a',)
Output
'a'
Notes
Minimum valid key: the single character is picked and the interval becomes empty.
Example 2
Input
('abc',)
Output
'cba'
Notes
Source example 1, strictly increasing: every character is used, largest first.

Constraints

  • •1 <= len(key) <= 10^5
  • •key contains only lowercase English letters 'a' to 'z'.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def largest_identifier(key) -> str:
Sample call
largest_identifier('a')
Language
Case 1
Input
('a',)
Expected
'a'
Case 2
Input
('abc',)
Expected
'cba'

Browse More Questions

Show the approach

Approach

Every later pick lies inside the part that was kept, so it sits on one side of every earlier pick, and each new interval is contained in the previous one. The first character of the best identifier must be the largest letter M of the current interval (any other first letter already loses at position 1), and every later pick comes from a sub-interval whose largest letter is at most M. So the best identifier never increases from left to right.

All m occurrences of M can be collected: peel them from the outside in, taking the leftmost remaining occurrence and keeping the right part, or the rightmost remaining occurrence and keeping the left part. More copies of M at the front are strictly better, so the best identifier starts with exactly m copies of M. Once they are taken, the interval is exactly one gap: the stretch before the first occurrence, between two consecutive occurrences, or after the last. Any gap can be reached by choosing which ends to peel from, but characters from two different gaps can never both be used, because that would require keeping both sides of some occurrence of M.

Therefore best(range) = M repeated m times, followed by the largest best(gap) over all gaps, where an empty gap contributes the empty string. Tails are compared as whole strings with the prefix rule, so a longer tail that extends a shorter one wins: in 'zbzabz' the gap 'ab' gives 'ba', which beats 'b' from the gap 'b'. A shorter tail can also win over a longer one ('cbzaaa' gives 'zcb', not 'zaaa'). Each gap's largest letter is strictly smaller than M, so the recursion is at most 26 levels deep.

Implementation: for the half-open range [lo, hi), find its largest letter, scan for its occurrences, recurse on every non-empty gap between them (including before the first and after the last), keep the largest tail, and return the repeated letter followed by that tail.

Edge cases: a single character; a key of one repeated letter (everything is kept); the largest letter at an end (one side is empty); strictly increasing or strictly decreasing keys (everything is kept); several gaps with equal tails (any of them yields the same string). Each character is scanned once per recursion level whose range contains it, at most 26 times, so the running time is O(26n). The tails held during the scan come from disjoint stretches of the key, so the extra space is O(n).

Time complexity:
O(26n)
Space complexity:
O(n)