PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Grammarly

Remove adjacent duplicates and handle tree input

Last updated: Mar 29, 2026

Quick Overview

This question evaluates string-processing and tree-construction skills, including efficient removal of adjacent or k-sized duplicate groups in strings and building a binary tree from a level-order array to compute properties such as the sum of left leaves.

  • medium
  • Grammarly
  • Coding & Algorithms
  • Software Engineer

Remove adjacent duplicates and handle tree input

Company: Grammarly

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are asked three related coding questions. ## 1) Remove adjacent duplicates (repeatedly) Given a string `s` (lowercase English letters), repeatedly remove any pair of **adjacent equal characters** until no such pair exists. - **Input:** `s` - **Output:** the final string after all removals **Example:** - `s = "abbaca"` → remove `bb` → `aaca` → remove `aa` → `ca` **Constraints (typical):** `1 <= |s| <= 1e5`. ## 2) Follow-up: remove groups of size k Given a string `s` and an integer `k`, repeatedly remove any **group of exactly `k` adjacent equal characters** until no more removals are possible. - **Input:** `s`, `k` - **Output:** final string **Example:** - `s = "deeedbbcccbdaa", k = 3` → output `"aa"` **Constraints (typical):** `1 <= |s| <= 1e5`, `2 <= k <= 1e4`. ## 3) Tree problem, but input is an array (must build the tree) You are given a binary tree in **level-order array form** (not `TreeNode` objects). `null` indicates a missing child. Build the binary tree and compute: > The **sum of all left leaf nodes** (a left leaf is a leaf node that is the left child of its parent). - **Input:** array `arr` of integers and `null`s, in level-order - **Output:** integer sum **Example:** - `arr = [3,9,20,null,null,15,7]` → left leaves are `9` and `15` → output `24` **Notes:** You must construct the tree from the array representation before computing the answer.

Quick Answer: This question evaluates string-processing and tree-construction skills, including efficient removal of adjacent or k-sized duplicate groups in strings and building a binary tree from a level-order array to compute properties such as the sum of left leaves.

Related Interview Questions

  • Implement a Transactional Key-Value Store - Grammarly (medium)
  • Merge overlapping corrections - Grammarly
  • Implement string reduction and time map - Grammarly (easy)
  • Solve interval merge and string dedup problems - Grammarly (hard)
Grammarly logo
Grammarly
Jan 22, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
7
0
Loading...

You are asked three related coding questions.

1) Remove adjacent duplicates (repeatedly)

Given a string s (lowercase English letters), repeatedly remove any pair of adjacent equal characters until no such pair exists.

  • Input: s
  • Output: the final string after all removals

Example:

  • s = "abbaca" → remove bb → aaca → remove aa → ca

Constraints (typical): 1 <= |s| <= 1e5.

2) Follow-up: remove groups of size k

Given a string s and an integer k, repeatedly remove any group of exactly k adjacent equal characters until no more removals are possible.

  • Input: s , k
  • Output: final string

Example:

  • s = "deeedbbcccbdaa", k = 3 → output "aa"

Constraints (typical): 1 <= |s| <= 1e5, 2 <= k <= 1e4.

3) Tree problem, but input is an array (must build the tree)

You are given a binary tree in level-order array form (not TreeNode objects). null indicates a missing child. Build the binary tree and compute:

The sum of all left leaf nodes (a left leaf is a leaf node that is the left child of its parent).

  • Input: array arr of integers and null s, in level-order
  • Output: integer sum

Example:

  • arr = [3,9,20,null,null,15,7] → left leaves are 9 and 15 → output 24

Notes: You must construct the tree from the array representation before computing the answer.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Grammarly•More Software Engineer•Grammarly Software Engineer•Grammarly Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.