Character After Special String Commands

Quick Overview

Return a requested zero-based character after a long sequence of append, removal, duplication, and reversal commands. The expanded text may be huge; an out-of-range index returns a period.

Character After Special String Commands

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

# Character After Special String Commands You are given a string `s` containing lowercase English letters and the commands `*`, `#`, and `%`, together with a zero-based index `k`. Start with empty working text. Process `s` from left to right: - A lowercase letter is appended to the text. - `*` removes the last character if one exists; on empty text it does nothing. - `#` appends a copy of the complete current text to itself. - `%` reverses the current text. Return the character at index `k` of the final text. If that index is outside the final text, return the character `'.'`. ### C++ Interface ```cpp class Solution { public: char processStr(string s, long long k); }; ``` ### Examples **Example 1** ```text s = "ab#%*c" k = 2 Output: 'b' ``` The successive texts are `"a"`, `"ab"`, `"abab"`, `"baba"`, `"bab"`, and `"babc"`. The character at zero-based index two is `b`. **Example 2** ```text s = "r#**" k = 0 Output: '.' ``` The texts are `"r"`, `"rr"`, `"r"`, and `""`. Index zero is outside the empty final text. ### Constraints - `1 <= s.length <= 100000`. - Every character is a lowercase English letter or one of `*`, `#`, `%`. - `0 <= k <= 1000000000000000`. - The final text length after processing all of `s` is at most `1000000000000000`. ### Clarifying Questions - Is `k` zero-based? Yes. The first character has index zero. - Is an index equal to the final length valid? No; return `'.'` for that index. - Does `#` duplicate the current text or the original input? The current text after all preceding operations. - Does reversal affect unprocessed input characters? No. It reverses only the working text. - Can deletion on empty text fail? No. It leaves the text empty. ### What a Strong Answer Covers - The exact command sequence and zero-based final-index semantics. - Correct results for empty final text and every out-of-range query. - Correct handling of repeated copies, reversals, and deletions in combination. - Resource and arithmetic bounds for 100000 input characters and final text lengths up to one quadrillion. ### Follow-up Questions - Can the final text be empty even when the input contains lowercase letters? Give an example. - Can the special out-of-range character `'.'` ever be part of a valid final text under these input rules? ```hint Follow one character's origin Record the exact length after every command without constructing the text. If the final index is valid, walk backward through the commands: map an index in either duplicate back to the original copy, mirror it across a reversal, and recognize when it points to an appended letter. ```

Overview: Return a requested zero-based character after a long sequence of append, removal, duplication, and reversal commands. The expanded text may be huge; an out-of-range index returns a period.

Amazon logo
Amazon
Apr 30, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Character After Special String Commands

You are given a string s containing lowercase English letters and the commands *, #, and %, together with a zero-based index k.

Start with empty working text. Process s from left to right:

  • A lowercase letter is appended to the text.
  • * removes the last character if one exists; on empty text it does nothing.
  • # appends a copy of the complete current text to itself.
  • % reverses the current text.

Return the character at index k of the final text. If that index is outside the final text, return the character '.'.

C++ Interface

class Solution {
public:
    char processStr(string s, long long k);
};

Examples

Example 1

s = "ab#%*c"
k = 2
Output: 'b'

The successive texts are "a", "ab", "abab", "baba", "bab", and "babc". The character at zero-based index two is b.

Example 2

s = "r#**"
k = 0
Output: '.'

The texts are "r", "rr", "r", and "". Index zero is outside the empty final text.

Constraints

  • 1 <= s.length <= 100000 .
  • Every character is a lowercase English letter or one of * , # , % .
  • 0 <= k <= 1000000000000000 .
  • The final text length after processing all of s is at most 1000000000000000 .

Clarifying Questions Guidance

  • Is k zero-based? Yes. The first character has index zero.
  • Is an index equal to the final length valid? No; return '.' for that index.
  • Does # duplicate the current text or the original input? The current text after all preceding operations.
  • Does reversal affect unprocessed input characters? No. It reverses only the working text.
  • Can deletion on empty text fail? No. It leaves the text empty.

What a Strong Answer Covers Guidance

  • The exact command sequence and zero-based final-index semantics.
  • Correct results for empty final text and every out-of-range query.
  • Correct handling of repeated copies, reversals, and deletions in combination.
  • Resource and arithmetic bounds for 100000 input characters and final text lengths up to one quadrillion.

Follow-up Questions Guidance

  • Can the final text be empty even when the input contains lowercase letters? Give an example.
  • Can the special out-of-range character '.' ever be part of a valid final text under these input rules?