Versioned Text Correction Previews
Updated
Problem statement
A text editor receives correction suggestions from a spelling and grammar model while a user types. Process the supplied ordered events and return a preview for every SUGGEST event.
For this exercise, assume the initial document has revision 0. An ["EDIT", newText] event replaces the entire current document and increases its revision by one, even if its text is unchanged. A ["SUGGEST", revision, start, end, replacement] event is the supplied model output for one half-open character range [start,end).
If the suggestion revision equals the current revision, return ["CURRENT", correctedText], replacing that range with replacement. A preview does not modify the current document or its revision. Otherwise return ["STALE", ""] and ignore the range. This also rejects a revision from the future. Preserve suggestion output order; EDIT events produce no output.
Function
previewVersionedTextCorrections(initialText: String, events: String[][]) → String[][]Examples
Example 1
initialText = "teh cat"events = [["SUGGEST","0","0","3","the"],["EDIT","teh cats"],["SUGGEST","0","0","3","the"],["SUGGEST","1","0","3","the"]]return = [["CURRENT","the cat"],["STALE",""],["CURRENT","the cats"]]The edit makes the delayed revision-zero prediction stale.
Example 2
initialText = "a b"events = [["SUGGEST","0","1","2",""],["SUGGEST","0","3","3","!"]]return = [["CURRENT","ab"],["CURRENT","a b!"]]Both previews use the unchanged document, because suggestions do not commit.
Example 3
initialText = ""events = [["EDIT","hi"],["SUGGEST","1","0","2","hello"]]return = [["CURRENT","hello"]]The first edit creates revision one.
Constraints
0 <= events.length <= 5000; every event has one of the exact row shapes above.- All text contains ASCII characters and each document or replacement has at most
2000characters. - Revision, start and end are nonnegative decimal strings. Revision is at most
5000, and0 <= start <= end <= 2000. - For a current suggestion,
end <= currentText.length. Stale suggestion ranges need not fit the current text. - The total number of characters in all current previews is at most
100000. Empty ranges insert text; empty replacements delete text.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEW
- File System Shell CommandsHarvey · ONSITE INTERVIEW
- Invoice / Payment ReconciliationStripe · PHONE SCREEN · ONSITE INTERVIEW
- Adjacent Pair and Run RemovalWhatNot · PHONE SCREEN · ONSITE INTERVIEW
- Feature Flags with Rules, Dependencies and ExposurePerplexity AI · ONSITE INTERVIEW
- Sliding Items on a Circular RingIDFC Bank · ONSITE INTERVIEW
- Stateful Log Transform and SearchRippling · PHONE SCREEN · ONSITE INTERVIEW
- Digit-Coded Card ValidationCapital One · ONSITE INTERVIEW
- Query JSON Records with WHERE and ORDER BYGitHub · ONSITE INTERVIEW
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW