Write the lexicographically smallest string through a robot's stack buffer

Quick Overview

Simulate a robot that moves characters from an input string into a stack-like buffer and writes them from the buffer's end, then find the lexicographically smallest output it can produce. It tests greedy reasoning on a stack and proving when an early choice is safe.

Write the lexicographically smallest string through a robot's stack buffer

Company: Salesforce

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a string `s` and a robot that currently holds an empty string `t`. Apply one of the following operations until `s` and `t` are both empty: - Remove the first character of `s` and give it to the robot. The robot appends this character to `t`. - Remove the last character of `t` and give it to the robot. The robot writes this character on paper. Return the lexicographically smallest string that can be written on paper. ### Function Signature ```python def robot_with_string(s: str) -> str: ... ``` ### Rules - The paper is finished only when both `s` and `t` are empty, so the returned string contains every character of `s` exactly once. - `t` only grows at its end and only shrinks from its end, so it behaves like a stack. ### Constraints - `1 <= len(s) <= 10^5` - `s` consists only of lowercase English letters. ### Examples ```text Input: s = "zza" Output: "azz" ``` The paper should start with `a`, the smallest letter. Since `a` is the last character of `s`, both `z` characters must already be in `t` when it is written, and they then come off the end of `t` one after the other. ```text Input: s = "bac" Output: "abc" ``` Push `b`, push `a`, write `a`, write `b`, then push `c` and write it. ```text Input: s = "bdda" Output: "addb" ``` Starting with `a` requires `b`, `d` and `d` to be in `t` first. After `a` is written, `t` holds `"bdd"` and empties from its end as `d`, `d`, `b`.

Overview: Simulate a robot that moves characters from an input string into a stack-like buffer and writes them from the buffer's end, then find the lexicographically smallest output it can produce. It tests greedy reasoning on a stack and proving when an early choice is safe.

Salesforce logo
Salesforce
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a string s and a robot that currently holds an empty string t. Apply one of the following operations until s and t are both empty:

  • Remove the first character of s and give it to the robot. The robot appends this character to t .
  • Remove the last character of t and give it to the robot. The robot writes this character on paper.

Return the lexicographically smallest string that can be written on paper.

Function Signature

def robot_with_string(s: str) -> str:
    ...

Rules

  • The paper is finished only when both s and t are empty, so the returned string contains every character of s exactly once.
  • t only grows at its end and only shrinks from its end, so it behaves like a stack.

Constraints

  • 1 <= len(s) <= 10^5
  • s consists only of lowercase English letters.

Examples

Input: s = "zza"
Output: "azz"

The paper should start with a, the smallest letter. Since a is the last character of s, both z characters must already be in t when it is written, and they then come off the end of t one after the other.

Input: s = "bac"
Output: "abc"

Push b, push a, write a, write b, then push c and write it.

Input: s = "bdda"
Output: "addb"

Starting with a requires b, d and d to be in t first. After a is written, t holds "bdd" and empties from its end as d, d, b.