PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates knowledge of data structure design, generic types, in-place memory allocation, and constant-time FIFO operations required for implementing a fixed-capacity circular buffer.

  • medium
  • Applied
  • Coding & Algorithms
  • Software Engineer

Implement a fixed-capacity generic circular buffer

Company: Applied

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Design a data structure that stores generic elements `T` with the following properties: - **FIFO** semantics (queue): `pop()` returns the oldest element. - `push()` and `pop()` must be **O(1)**. - The buffer has a fixed **capacity** known at class instantiation time, and **memory must be allocated at instantiation** (no per-operation allocation). - When the buffer is full and you `push()` a new element, it **overwrites the oldest** element. ### Task Implement a circular buffer class, e.g. `CircularBuffer<T, N>` where: - `T` is the element type - `N` is the fixed capacity (example: `N = 5`) Implement at least: - `push(value)` - `pop()` - `print()` (prints elements from oldest to newest) ### Define edge behavior - What happens if `pop()` is called on an empty buffer? - After overwriting, ensure ordering is still FIFO over the retained elements.

Quick Answer: This question evaluates knowledge of data structure design, generic types, in-place memory allocation, and constant-time FIFO operations required for implementing a fixed-capacity circular buffer.

Simulate a circular buffer. push overwrites the oldest item when full; pop removes oldest; peek returns oldest; snapshot returns contents oldest to newest. Return outputs from pop, peek, and snapshot.

Constraints

  • Inputs are provided as Python literals compatible with the function signature.
  • Return a deterministic value exactly matching the requested output.

Examples

Input: (3, [['push', 1], ['push', 2], ['push', 3], ['snapshot'], ['push', 4], ['snapshot'], ['pop'], ['peek']])

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

Explanation: Overwrite oldest.

Input: (2, [['pop'], ['push', 'a'], ['peek'], ['push', 'b'], ['push', 'c'], ['snapshot']])

Expected Output: [None, 'a', ['b', 'c']]

Explanation: Empty pop and strings.

Input: (0, [['push', 1], ['snapshot']])

Expected Output: []

Explanation: Zero capacity.

Hints

  1. Start with a direct data structure representation.
  2. Handle edge cases before the main loop.
Last updated: Jun 27, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ 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
  • AI Coding 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

  • Multi-Agent Collision Simulator (Unicycle Kinematics) - Applied (medium)
  • Merge Overlapping Collinear Segments - Applied (hard)
  • Implement a Fixed-Capacity Deque - Applied (medium)
  • Implement Nested Transactional Key-Value Store - Applied (hard)
  • Merge overlapping 2D line segments - Applied (medium)