PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of data structures and buffer management, specifically FIFO semantics, fixed-capacity storage, wraparound indexing, and careful edge-case handling.

  • medium
  • Jane Street
  • Coding & Algorithms
  • Software Engineer

Implement a Circular Buffer

Company: Jane Street

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Implement a fixed-capacity circular buffer. A circular buffer stores elements in insertion order, supports removal of the oldest element, and reuses storage positions when the end of the underlying array is reached. Design and implement a `CircularBuffer` with the following behavior: - `CircularBuffer(capacity)`: create a buffer that can hold at most `capacity` elements. - `enqueue(value)`: insert `value` at the back of the buffer. If the buffer is full, return `false` or otherwise clearly indicate failure. - `dequeue()`: remove and return the oldest element. If the buffer is empty, return `null` or otherwise clearly indicate failure. - `peek()`: return the oldest element without removing it. - `size()`: return the current number of elements. - `isEmpty()`: return whether the buffer is empty. - `isFull()`: return whether the buffer is full. You may assume time complexity is not the main focus; prioritize correct functionality and clean handling of edge cases such as empty buffers, full buffers, capacity one, and wraparound.

Quick Answer: This question evaluates understanding of data structures and buffer management, specifically FIFO semantics, fixed-capacity storage, wraparound indexing, and careful edge-case handling.

Implement a fixed-capacity circular buffer by simulating its operations. Because this platform evaluates a function rather than a class, write a function `solution(capacity, operations)` that processes a sequence of circular buffer operations and returns the result of each operation in order. The circular buffer stores elements in FIFO order and reuses array positions when it reaches the end. Supported operations: - `('enqueue', value)`: insert `value` at the back. Return `True` if successful, or `False` if the buffer is full. - `('dequeue',)`: remove and return the oldest element. Return `None` if the buffer is empty. - `('peek',)`: return the oldest element without removing it. Return `None` if the buffer is empty. - `('size',)`: return the current number of elements. - `('isEmpty',)`: return `True` if the buffer is empty, otherwise `False`. - `('isFull',)`: return `True` if the buffer is full, otherwise `False`. Your goal is to correctly handle normal cases and edge cases such as empty buffers, full buffers, capacity one, and wraparound behavior.

Constraints

  • 1 <= capacity <= 10^5
  • 0 <= len(operations) <= 2 * 10^5
  • Values used in `enqueue` are integers in the range [-10^9, 10^9]
  • Each operation should be processed in O(1) time

Examples

Input: (3, [('enqueue', 10), ('enqueue', 20), ('peek',), ('dequeue',), ('size',), ('isEmpty',), ('isFull',)])

Expected Output: [True, True, 10, 10, 1, False, False]

Explanation: After inserting 10 and 20, the oldest value is 10. Dequeue removes 10, leaving one element. The buffer is neither empty nor full.

Input: (3, [('enqueue', 1), ('enqueue', 2), ('enqueue', 3), ('isFull',), ('dequeue',), ('enqueue', 4), ('peek',), ('dequeue',), ('dequeue',), ('dequeue',), ('isEmpty',)])

Expected Output: [True, True, True, True, 1, True, 2, 2, 3, 4, True]

Explanation: This tests wraparound. After removing 1, inserting 4 reuses the freed slot at the front of the underlying array.

Input: (2, [('enqueue', 5), ('enqueue', 6), ('enqueue', 7), ('peek',), ('size',), ('dequeue',), ('enqueue', 7), ('isFull',)])

Expected Output: [True, True, False, 5, 2, 5, True, True]

Explanation: The third enqueue fails because the buffer is full. After one dequeue, 7 can be inserted successfully.

Input: (1, [('isEmpty',), ('enqueue', 9), ('isFull',), ('peek',), ('enqueue', 10), ('dequeue',), ('dequeue',), ('isEmpty',)])

Expected Output: [True, True, True, 9, False, 9, None, True]

Explanation: With capacity one, the buffer becomes full after a single enqueue. A second enqueue fails, and dequeue empties the buffer again.

Input: (4, [])

Expected Output: []

Explanation: No operations means no results.

Input: (3, [('enqueue', -1), ('enqueue', -1), ('dequeue',), ('peek',), ('size',)])

Expected Output: [True, True, -1, -1, 1]

Explanation: This shows that negative numbers and duplicate values should be handled correctly.

Hints

  1. Track the index of the oldest element and the current number of elements. You do not need to shift elements after a dequeue.
  2. When moving to the next slot, use modulo arithmetic so indices wrap back to the beginning of the array.
Last updated: Jun 6, 2026

Loading coding console...

PracHub

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

Related Coding Questions

  • Optimize trade PnL table updates - Jane Street (hard)
  • Transform sparse time-code stream to dense rows - Jane Street (easy)
  • Sort trade executions into a canonical order - Jane Street (easy)
  • Solve queue switch and coin change puzzles - Jane Street (medium)
  • Implement Connect Four with bottom push-up - Jane Street (Medium)