← Grammarly Interview Insights

Grammarly·Software Engineer·Technical Phone Screen·Senior

Senior
Jun 2026

Summary

Grammarly software engineer interview with a pretty involved algorithmic problem around merging overlapping text correction spans with custom tie-breaking rules. One question, but it had enough layers to keep me busy for a while.

Questions Asked (1)

Q1

Implement a function that takes a list of text-correction spans (each with a start index, end index, priority, and correction type) and returns a non-overlapping list of corrections. When spans overlap, the higher-priority one wins; ties go to the shorter original span; further ties go to earlier input order. If a correction loses only part of its range, split it into the surviving pieces, but each fragment must still carry the metadata of the original span for future comparisons.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This one got me.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the requirements and edge cases, then propose a sweep-line algorithm that processes events in order of position, using a priority queue to manage active spans. Explain how to resolve overlaps by priority, length, and input order, and how to split losing spans while preserving metadata. Finally, analyze time and space complexity and discuss potential optimizations.

Pro tip: Emphasize that splitting a span must preserve its original metadata (priority, type, input order) so that future comparisons remain consistent; this shows attention to detail and understanding of the problem's invariants.

1. Clarify requirements and edge cases

Ask about input constraints (e.g., number of spans, index range), expected output format, and how to handle invalid spans (e.g., start >= end). Confirm that splitting should retain original metadata.

2. Choose an algorithm

Propose a sweep-line approach: create events for span starts and ends, sort them by position, and use a priority queue to track active spans. Alternatively, consider interval tree or divide-and-conquer if appropriate.

3. Define comparison and resolution logic

Specify the comparator for spans: higher priority wins; if equal, shorter original length wins; if still equal, earlier input order wins. Explain how to handle partial overlaps by splitting the losing span.

4. Implement and handle splitting

Walk through the algorithm: at each event, update active set, determine the winning span for the current segment, and if a span is partially overlapped, split it into surviving fragments, each retaining the original span's metadata.

5. Analyze complexity and test

State time complexity (e.g., O(n log n) for sorting and heap operations) and space complexity. Discuss test cases: no overlaps, full overlaps, partial overlaps, ties, and edge cases like zero-length spans.

Key Points to Mention

  • Sweep-line algorithm with events for span boundaries
  • Priority queue (or balanced tree) to manage active spans efficiently
  • Comparator: priority > shorter original length > earlier input order
  • Splitting spans while preserving original metadata (priority, type, input order)
  • Handling of partial overlaps and generation of non-overlapping output
  • Time and space complexity analysis (e.g., O(n log n) time, O(n) space)

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