PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches
|Home/Coding & Algorithms/Google

Implement Memory-Efficient Document Undo/Redo

Last updated: Apr 12, 2026

Quick Overview

This question evaluates data structure design and algorithmic reasoning for implementing memory-efficient undo/redo semantics on a key-value document, emphasizing space-efficient per-batch state tracking (storage proportional to distinct keys changed) and correct handling of key creation and deletion.

  • easy
  • Google
  • Coding & Algorithms
  • Software Engineer

Implement Memory-Efficient Document Undo/Redo

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

You are implementing a simplified document layer for a design tool. The document is a map from `string key` to `string value`. Implement a data structure that supports these operations: - `apply(key, value)`: immediately update the document so that `document[key] = value`. - `batch()`: close the current batch of changes. All `apply` calls since the previous `batch()` belong to one undoable unit. - `undo()`: revert the most recently closed batch. - `redo()`: reapply the most recently undone batch. Use standard undo/redo semantics: - `apply()` takes effect immediately. - A batch may contain multiple `apply()` calls. - `undo()` reverts the entire last batch at once. - `redo()` reapplies the same batch. - If a new batch is created after an `undo()`, the redo history is cleared. The main requirement is **memory efficiency**. If the same key is updated multiple times within one batch, you should not store every intermediate change. Instead, the memory used by one batch should be proportional to the number of **distinct keys changed in that batch**, not the total number of `apply()` calls. Example: - Initial document: `{ "color": "red" }` - `apply("color", "yellow")` - `apply("color", "pink")` - `batch()` - `undo()` should restore the document to `{ "color": "red" }` - `redo()` should change it back to `{ "color": "pink" }` Also handle keys that did not exist before the batch. If a batch creates a new key, then `undo()` should remove that key entirely. Design the data structures and implement these operations. Discuss the time and space complexity.

Quick Answer: This question evaluates data structure design and algorithmic reasoning for implementing memory-efficient undo/redo semantics on a key-value document, emphasizing space-efficient per-batch state tracking (storage proportional to distinct keys changed) and correct handling of key creation and deletion.

Related Interview Questions

  • Solve Flower Placement and Directory Deletion - Google (medium)
  • Compute Turnstile Crossing Times - Google (hard)
  • Simulate In-Place Cellular State Updates - Google (hard)
  • Determine Whether a Word Exists in a Graph - Google (medium)
  • Implement Checksums and Feature Rollout Evaluation - Google (medium)
Google logo
Google
Jan 12, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
3
0
Loading...

You are implementing a simplified document layer for a design tool. The document is a map from string key to string value.

Implement a data structure that supports these operations:

  • apply(key, value) : immediately update the document so that document[key] = value .
  • batch() : close the current batch of changes. All apply calls since the previous batch() belong to one undoable unit.
  • undo() : revert the most recently closed batch.
  • redo() : reapply the most recently undone batch.

Use standard undo/redo semantics:

  • apply() takes effect immediately.
  • A batch may contain multiple apply() calls.
  • undo() reverts the entire last batch at once.
  • redo() reapplies the same batch.
  • If a new batch is created after an undo() , the redo history is cleared.

The main requirement is memory efficiency. If the same key is updated multiple times within one batch, you should not store every intermediate change. Instead, the memory used by one batch should be proportional to the number of distinct keys changed in that batch, not the total number of apply() calls.

Example:

  • Initial document: { "color": "red" }
  • apply("color", "yellow")
  • apply("color", "pink")
  • batch()
  • undo() should restore the document to { "color": "red" }
  • redo() should change it back to { "color": "pink" }

Also handle keys that did not exist before the batch. If a batch creates a new key, then undo() should remove that key entirely.

Design the data structures and implement these operations. Discuss the time and space complexity.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

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

Master your tech interviews with 7,500+ 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.