Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    DSADSA

    Foundations: measuring code

    • Big-O: how code grows
    • Space and the time–memory trade-off
    • Arrays and strings under the hood
    • Hash maps and sets
    • How to approach any interview problem

    Array patterns

    • Two pointers
    • Sliding window
    • Prefix sums
    • Binary search, done right
    • Sorting: merge sort and quicksort

    Stacks, queues and linked lists

    • Stacks and monotonic stacks
    • Queues and deques
    • Linked lists

    Recursion and trees

    • Recursion without fear
    • Backtracking: try and undo
    • Binary trees
    • Binary search trees
    • Heaps and priority queues

    Graphs and dynamic programming

    • Graphs: BFS and DFS
    • Shortest paths, topological sort and union-find
    • Dynamic programming: the idea
    • DP on grids and strings
    • Greedy: the best choice right now
    DSA

    Stacks, queues and linked lists · Lesson 12 of 23

    Queues and deques

    First in, first out, why `shift()` slows code down, and deques for sliding windows.

    11 minutes2 games

    A queue is a line: join at the back, leave from the front (FIFO). It powers BFS, job queues, and anything served in order.

    A classic JavaScript trap: arr.shift() removes the first element but shifts all the rest: O(n). Use it in a BFS over 100,000 items and you get O(n²).

    JavaScript
    // A queue with O(1) dequeue: keep a head index instead of shifting
    class Queue {
      constructor() { this.items = []; this.head = 0 }
      enqueue(x) { this.items.push(x) }
      dequeue() { return this.items[this.head++] }
      get size() { return this.items.length - this.head }
    }
    
    const q = new Queue()
    q.enqueue("Mona"); q.enqueue("Omar"); q.enqueue("Laila")
    console.log(q.dequeue(), q.dequeue(), q.size)

    enqueue at the back

    [Mona, Omar, Laila]

    dequeue from the front

    Deque: both ends

    A deque (double-ended queue) adds and removes at both ends. Its star use: the maximum of every window of size k. Keep indexes whose values decrease: anything smaller than the newcomer is useless (the newcomer is bigger and lives longer), so drop it from the back; anything that slid out of the window drops from the front. The maximum is always first.

    JavaScript
    function windowMax(nums, k) {
      const dq = [], out = []
      let head = 0
      for (let i = 0; i < nums.length; i++) {
        while (dq.length > head && nums[dq[dq.length - 1]] <= nums[i]) dq.pop()   // drop smaller from the back
        dq.push(i)
        if (dq[head] <= i - k) head++                                             // drop the expired front
        if (i >= k - 1) out.push(nums[dq[head]])
      }
      return out
    }
    
    console.log(windowMax([1, 3, -1, -3, 5, 3, 6, 7], 3))

    Important

    In an interview you may say "I'll use an array with shift as a queue" as long as you mention it's O(n) and that the proper fix is a head index or a linked list. Other languages ship real queues: Python's collections.deque, C++'s std::queue/std::deque.

    Quick question

    An empty queue: enqueue(1), enqueue(2), dequeue(), enqueue(3), dequeue(). What did the last dequeue return?

    Interview question

    Implement a queue using only two stacks.

    Exercise

    Use the Queue class (included) to simulate a ticket line: each person needs some tickets, buys one, and rejoins the back if they still need more. Print the order people finish. It must print Omar Mona Laila.

    Expected output: Omar Mona Laila

    JavaScript
    class Queue {
      constructor() { this.items = []; this.head = 0 }
      enqueue(x) { this.items.push(x) }
      dequeue() { return this.items[this.head++] }
      get size() { return this.items.length - this.head }
    }
    
    const q = new Queue()
    q.enqueue({ name: "Mona", need: 2 })
    q.enqueue({ name: "Omar", need: 1 })
    q.enqueue({ name: "Laila", need: 3 })
    const done = []
    // your loop here
    
    console.log(done.join(" "))

    Practise on real problems

    Practice problems that use what you learned here. Solve them in JavaScript, Python or C++.

    • Sliding window maximumHard
    • Rotting orangesMedium
    • Shortest path in a mazeMedium

    Learn more from

    • Wikipedia: Queue (abstract data type)en.wikipedia.org
    • Wikipedia: Double-ended queueen.wikipedia.org
    • Princeton Algorithms: Bags, Queues, and Stacksalgs4.cs.princeton.edu

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousStacks and monotonic stacksNextLinked lists
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.