Flag Accidentally Repeated Words in a Draft

Last updated: November 29, 2026

Quick Overview

Given a block of text, return the character span of every run where the same word appears two or more times in a row separated only by whitespace (for example 'the the'), compared case-insensitively, along with the word and how many times it repeats.

Grammarly
Coding & Algorithms
Software Engineer
Grammarly
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Given a block of text, return the character span of every run where the same word appears two or more times in a row separated only by whitespace (for example 'the the'), compared case-insensitively, along with the word and how many times it repeats.

A warm-up in the spirit of a lightweight rule-based check that sits next to Grammarly's ML models. The interviewer wants clean tokenization with positions preserved, correct handling of punctuation between words, and a sensible output contract for the UI.

What the Interviewer Expects
  • Tokenize while keeping character offsets, not just the word list
  • Require that only whitespace separates the two words, so 'that, that' is not flagged
  • Merge three or more repeats into one run instead of overlapping spans
  • Compare case-insensitively but report original spans
  • Run in a single linear pass
Key Topics to Cover
regex tokenization
character offsets
linear scan
run-length grouping
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Some repeats are grammatical ('had had', 'that that'). How would you avoid flagging them?
  • How would you handle a repeat split across a line break inside a list item or a code block?
  • How would this run incrementally as the user types?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Words are letters with an optional internal apostrophe (don't). Two neighbors form a repeat when the gap between them is non-empty and all whitespace (spaces, tabs, newlines) and their lowercased fo...

High-Level Design

Walk word matches with re.finditer, which gives start() and end(). Keep the previous match and a counter for the current run. When the current word repeats the previous one, grow the run; otherw...


Submit Your Answer
Markdown supported

Related Questions