Implement Deque with with follow-up

Last updated: December 1, 2025

Quick Overview

Implement a deque (double-ended queue) data structure that supports the following operations: addFirst, addLast, removeFirst, removeLast, getFirst, and getLast. Each operation should have a time complexity of O(1). Your implementation should also handle edge cases, such as operations on an empty deque, and should include methods to check if the deque is empty or to retrieve its size.

Google
Coding & Algorithms
Software Engineer
Google
December 1, 2025
Software Engineer
Take-home Project
Coding & Algorithms
Hard

281

5

2,063 solved


Implement a deque (double-ended queue) data structure that supports the following operations: addFirst, addLast, removeFirst, removeLast, getFirst, and getLast. Each operation should have a time complexity of O(1). Your implementation should also handle edge cases, such as operations on an empty deque, and should include methods to check if the deque is empty or to retrieve its size.

This coding problem is frequently asked during Take-home Project at Google. The interviewer is testing your ability to translate a problem into clean, working code while discussing time and space complexity. Google expects candidates to write production-quality code, not just solve the puzzle.

What the Interviewer Expects
  • Quickly identify the optimal approach and its theoretical basis
  • Handle complex algorithm design with multiple interacting components
  • Write concise, elegant code under time pressure
  • Prove correctness of your approach and discuss alternative solutions
  • Optimize beyond the obvious: discuss constant factor improvements
  • Address follow-up variations and explain how the solution generalizes
Key Topics to Cover
Edge cases and input validation
Data structure selection and trade-offs
Sorting and searching
Binary search and divide and conquer
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 parallelize this solution?
  • What is the worst-case input for your solution?
  • Can you solve this iteratively instead of recursively (or vice versa)?
  • How would your solution change if the input was sorted?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Problem Analysis

To implement a deque that supports operations like addFirst, addLast, removeFirst, and removeLast in O(1) time complexity, we can utilize a doubly linked list. This data structure allows for efficient...

Approach
  1. Node Class: Create a Node class that contains a value, a pointer to the previous node, and a pointer to the next node.
  2. Deque Class: Create a Deque class with pointers to the head and ta...

Submit Your Answer
Markdown supported

Related Questions