Implement Priority Queue with O(1) space

Last updated: October 20, 2025

Quick Overview

Implement a priority queue data structure that supports insertion and extraction of the highest priority element in O(log n) time while using O(1) additional space. The priority queue should allow for elements to be added with a specified priority and should return the element with the highest priority when extracted. Ensure that the implementation handles edge cases, such as extracting from an empty queue.

Citadel
Coding & Algorithms
Software Engineer
Citadel
October 20, 2025
Software Engineer
Technical Screen
Coding & Algorithms
Hard

132

7

4,598 solved


Implement a priority queue data structure that supports insertion and extraction of the highest priority element in O(log n) time while using O(1) additional space. The priority queue should allow for elements to be added with a specified priority and should return the element with the highest priority when extracted. Ensure that the implementation handles edge cases, such as extracting from an empty queue.

This coding problem is frequently asked during Technical Screen at Citadel. The interviewer is testing your ability to translate a problem into clean, working code while discussing time and space complexity. Citadel 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
Tree structures and recursion
Data structure selection and trade-offs
Common algorithm patterns (sliding window, two pointers, BFS/DFS)
Binary search and divide and conquer
Edge cases and input validation
Time and space complexity analysis
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 modify your solution to handle streaming input?
  • How would you test this solution thoroughly?
  • 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 priority queue with O(1) additional space, we can leverage a binary heap structure, specifically a max-heap, where each parent node has a higher priority than its children. The operatio...

Approach
  1. Data Structure: Use a list to represent the heap. The first element will act as the root of the heap.
  2. Insertion: When adding a new element, append it to the end of the list and 'bubble ...

Submit Your Answer
Markdown supported

Related Questions