Rebase Pending Writing Suggestions Through Live Document Edits

Last updated: November 29, 2026

Quick Overview

Suggestions were computed against an older snapshot of a document as (start, end, replacement) spans. Given the sequence of edits the user has made since then, each as (pos, delete_len, insert_text), return the suggestions that are still valid with offsets correct for the current text, dropping any whose flagged text was touched.

Grammarly
Coding & Algorithms
Software Engineer
Grammarly
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Suggestions were computed against an older snapshot of a document as (start, end, replacement) spans. Given the sequence of edits the user has made since then, each as (pos, delete_len, insert_text), return the suggestions that are still valid with offsets correct for the current text, dropping any whose flagged text was touched.

Grammarly's checker runs on the server while the user keeps typing, so every suggestion card arrives slightly stale and has to be re-anchored before an underline is drawn. The interviewer is checking careful interval reasoning at boundaries (inserts exactly at a span's start or end) and whether you can reason about correctness before reaching for a heavier data structure.

What the Interviewer Expects
  • Define precisely when an edit is before, after, or overlapping a span, including zero-length inserts at the boundaries
  • Apply edits in order, since each edit's position refers to the text produced by the previous one
  • Shift spans by len(insert) minus delete_len only when the whole edit precedes them
  • Drop rather than guess when an edit lands inside flagged text
  • State O(S x E) cost and discuss a faster structure for large documents
Key Topics to Cover
interval arithmetic
offset rebasing
text editing deltas
boundary conditions
Fenwick tree
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
  • How would you handle thousands of suggestions and a fast typist without rescanning every span per keystroke?
  • Should an insert exactly at a span's end extend the span (the user is still typing the word) or not?
  • How would you re-validate a dropped suggestion cheaply instead of waiting for a full server recheck?
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

Input: suggestions against the original text and an ordered list of edits against the live text. Output: surviving suggestions re-expressed in final-text offsets. Each span is half-open [start, end)...

High-Level Design

Process edits one at a time, because edit positions are only meaningful relative to the text after the previous edit. For each live suggestion classify the edit:

  • Entirely before (`pos + delete_...

Submit Your Answer
Markdown supported

Related Questions