Magicsheet logo

Faulty Keyboard

Easy
83.2%
Updated 6/1/2025

Asked by 2 Companies

Faulty Keyboard

What is this problem about?

The Faulty Keyboard interview question describes a keyboard with a strange glitch: every time you type the character 'i', the entire string you've typed so far is reversed. For example, if you want to type "string", you would type 's', 't', 'r'... then 'i' causes "str" to become "rts". Your task is to return the final string after all characters have been processed.

Why is this asked in interviews?

Companies like Samsung and Microsoft ask this String and Simulation coding problem to evaluate your basic programming logic and string manipulation skills. While the problem is "Easy," it tests whether you can implement a simulation efficiently without making common errors. It’s also an opportunity to discuss the time complexity of string reversals.

Algorithmic pattern used

This is a straightforward Simulation problem.

  1. Initialize an empty result (using a list or dynamic array).
  2. Iterate through each character in the input string.
  3. If the character is not 'i', append it to the result.
  4. If the character is 'i', reverse the entire current result.
  5. Join the characters into a final string and return.

Example explanation

Input: "poi"

  1. Type 'p': result = ['p'].
  2. Type 'o': result = ['p', 'o'].
  3. Type 'i': reverse ['p', 'o'] -> ['o', 'p']. Final result: "op".

Input: "string"

  1. Type 's', 't', 'r'. result = "str".
  2. Type 'i': reverse "str" -> "rts".
  3. Type 'n', 'g'. result = "rtsng". Final result: "rtsng".

Common mistakes candidates make

  • Inefficient Reversal: Reversing a string inside a loop using s = s[::-1] in Python or string concatenation in Java, which can lead to O(N^2) complexity. Using a mutable list/StringBuilder is better.
  • Missing the 'i': Accidentally including the 'i' character in the final string.
  • Handling Empty Strings: Not checking for edge cases where the first character is 'i'.

Interview preparation tip

Mention that for strings with many 'i' characters, you could potentially optimize the simulation using a Deque (Double-Ended Queue) and a "reverse flag." Instead of physically reversing, you just switch which end you append to.

Similar Questions