Print the Smallest String through a Last-In-First-Out Robot Buffer
Company: Bloomberg
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
### Problem
Start with input `s`, an empty robot buffer t, and empty paper. You may move the first remaining input character to the end of t, or remove the last character of t and append it to the paper. Continue until both s and t are empty. Return the lexicographically smallest possible paper string.
```cpp
class Solution {
public:
string robotWithString(string s);
};
```
### Examples
**Example 1:**
```text
Input: s = "cba"
Output: "abc"
Explanation: Push all three characters, then pop them in reverse order.
```
**Example 2:**
```text
Input: s = "abac"
Output: "aabc"
Explanation: Print the first a immediately, then push b and a; print a then b before reading and printing c.
```
### Constraints
- `1 <= s.length <= 100000`.
- s contains lowercase English letters.
### Clarifying Questions
- Which input character may be taken next? Only the first remaining one.
- Which buffer character may be printed? Only the last one.
- Can any character be discarded? No; both input and buffer must end empty.
- Is any valid output acceptable? No; return the lexicographically smallest.
```hint Check the boundary
Check the order of characters after putting several into the buffer before printing any.
```
Overview: Find the lexicographically smallest string printable by moving input characters into a buffer and printing only its last character.
Print the Smallest String through a Last-In-First-Out Robot Buffer
Bloomberg
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0
Problem
Start with input s, an empty robot buffer t, and empty paper. You may move the first remaining input character to the end of t, or remove the last character of t and append it to the paper. Continue until both s and t are empty. Return the lexicographically smallest possible paper string.
class Solution {
public:
string robotWithString(string s);
};
Examples
Example 1:
Input: s = "cba"
Output: "abc"
Explanation: Push all three characters, then pop them in reverse order.
Example 2:
Input: s = "abac"
Output: "aabc"
Explanation: Print the first a immediately, then push b and a; print a then b before reading and printing c.
Constraints
1 <= s.length <= 100000
.
s contains lowercase English letters.
Clarifying Questions Guidance
Which input character may be taken next? Only the first remaining one.
Which buffer character may be printed? Only the last one.
Can any character be discarded? No; both input and buffer must end empty.
Is any valid output acceptable? No; return the lexicographically smallest.