← Grammarly Interview Insights

Grammarly·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Interviewed for a software engineering role at Grammarly and got a string manipulation problem that looks deceptively simple until you try to do it without a stack. The whole thing hinged on whether you knew the right data structure going in.

Questions Asked (1)

Q1

Given a string of lowercase letters, repeatedly remove adjacent duplicate characters until no more removals are possible. Return the final string. Walk through an O(n) solution.

Algorithms & Data Structures
Author's notes

The naive approach of looping and rescanning the string will get you there eventually but it's slow and they'll push back.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem: removing adjacent duplicates repeatedly until no more removals are possible. Then propose a stack-based solution that processes the string in one pass, pushing characters and popping when the top of the stack matches the current character. Walk through a small example to illustrate, then analyze time and space complexity.

Pro tip: Mention that the stack approach naturally handles cascading removals because after popping, the new top can match the next character, effectively simulating the repeated removal process in a single pass. Also, note that the output order is preserved by the stack.

1. Clarify the problem

Confirm that removals are applied repeatedly until no adjacent duplicates remain, and that the final string should be returned. Ask if the input can be empty or if there are constraints on length.

2. Propose a stack-based approach

Explain that a stack can efficiently track characters, and when the current character matches the top of the stack, we pop; otherwise, we push. This simulates the removal process in one pass.

3. Walk through an example

Use a string like 'abbaca' to demonstrate: push 'a', push 'b', see 'b' matches top, pop, then 'a' matches top, pop, push 'c', push 'a' -> 'ca'. Show how cascading removals are handled.

4. Analyze complexity

State that each character is pushed and popped at most once, so time complexity is O(n). Space complexity is O(n) for the stack in the worst case.

5. Discuss edge cases and alternatives

Mention empty string, all duplicates, and no duplicates. Optionally, note that a two-pointer approach can achieve O(1) extra space if the input is mutable, but the stack is simpler and clearer.

Key Points to Mention

  • Stack data structure for O(n) time complexity
  • Single pass through the string
  • Cascading removals handled naturally by stack
  • Time complexity: O(n), Space complexity: O(n)
  • Edge cases: empty string, all same characters, no duplicates
  • Alternative in-place two-pointer approach for O(1) space

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.