← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Google SWE coding round, one design question about a Bookshelf class. Pretty standard OOP stuff but they wanted test cases too, which I half-expected and half-forgot to prep for properly.

Questions Asked (1)

Q1

Design a Bookshelf class that supports adding a book, removing a book by title, and moving a book to a specific position. Book titles are unique. Write at least three test cases after your implementation.

Algorithms & Data StructuresSystem Design
Author's notes

The core implementation wasn't bad.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify requirements first, then choose a data structure that balances O(1) title lookup with efficient positional moves, such as a doubly linked list plus a hash map. Implement the class with clean APIs, handle edge cases, and write tests covering normal operations, boundary conditions, and error cases.

Pro tip: Explicitly discuss the trade-offs between using an array (O(n) moves) and a linked list (O(1) moves but O(n) lookup) to show you understand performance implications, and mention that in a real system you might use a balanced tree or order-statistic tree for O(log n) operations.

1. Clarify requirements and constraints

Ask about expected input sizes, whether positions are 0-indexed or 1-indexed, and what should happen when removing or moving a non-existent book. Confirm that titles are unique and that moving a book to its current position is a no-op.

2. Choose data structures and justify

Select a combination like a hash map for O(1) title-to-node lookup and a doubly linked list for O(1) insertion, removal, and repositioning. Explain why this beats a simple array or a single list.

3. Implement the class with clean methods

Write methods addBook(title), removeBook(title), and moveBook(title, newPosition). Handle edge cases such as empty shelf, invalid position, and duplicate titles. Ensure moveBook correctly updates links.

4. Write comprehensive test cases

Create at least three tests: one for adding and removing books, one for moving a book to the front/back/middle, and one for error conditions like removing a non-existent book or moving to an invalid position.

5. Analyze complexity and discuss improvements

State time and space complexity for each operation. Mention potential optimizations like using an order-statistic tree for O(log n) moves if positions are frequent, or a skip list for simpler implementation.

Key Points to Mention

  • Use a hash map for O(1) title lookup and a doubly linked list for O(1) insertions, removals, and moves.
  • Handle edge cases: empty shelf, removing/moving non-existent book, invalid position, moving to same position.
  • Ensure moveBook correctly detaches the node and reinserts it at the target position, updating pointers.
  • Write tests that cover normal operations, boundary conditions (first/last position), and error cases.
  • Analyze time complexity: add O(1), remove O(1), move O(1) with the chosen structures.
  • Discuss trade-offs and potential improvements for large-scale or position-heavy scenarios.

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