Lesson overview · Free interview practice

Queues

Understand the FIFO principle and how queues are implemented.

Topics in the full lesson
  • Concept and Use Cases
  • When to Use
  • Time and Space Complexity
  • Queue Operations and Methods
  • Practical Tips and Tricks
  • Common Gotchas
  • Advanced Topics
  • Queue Algorithms
  • Interview Tips and Tricks
  • Common Mistakes

Try the free interview questions

Work through a question before revealing its explanation. These questions and answers are free; Premium adds the full lesson walkthrough, examples and implementation detail.

Question

Design a RecentCounter class to count the number of recent requests within a 3000ms sliding window. The class should have one method ping(t: number) that records a request at time t (t is strictly increasing across calls) and returns the number of requests in the inclusive range [t - 3000, t]. Use a queue (FIFO) to drop requests that have aged out — pop from the front in amortized O(1).

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

CodingMedium

Perfect Squares

Question

Given an integer n, return the least number of perfect square numbers (for example 1, 4, 9, 16) that sum to n. A perfect square is an integer that is the square of an integer.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

Question

Design a circular queue of fixed capacity k. Implement enQueue(value) (returns false if full), deQueue() (returns false if empty), Front() and Rear() (return -1 if empty), isEmpty(), and isFull().

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

CodingMedium

Design Hit Counter

Question

Design a hit counter that counts hits received in the past 5 minutes (300 seconds). Implement hit(timestamp) to record a hit at the given time and getHits(timestamp) to return the number of hits in the previous 300 seconds (inclusive). Calls arrive in nondecreasing timestamp order.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

Explore the full Queues material

Premium includes the complete lessons and implementation references. Free practice questions remain available without a subscription.

All course tracks & premium content
From basics to advanced masterclasses
Built for JS/TS developers like you
Real-world tips & common pitfalls
Upgrade to Premium