Queues

A container of objects that are inserted and removed according to the first-in, first-out principle.

Section Menu

How do I use this section?

1. CHALLENGE

Bottom Left Node Value

Assume we're using a binary tree in writing a video game via Binary Space Partitioning. We need to identify the bottom left leaf, that is-- the leftmost value in the lowest row of the binary tree. <img src="https://...

2. CHALLENGE

Mean Per Level

We are given a binary tree and are tasked with writing a method that determines the average of every level in the tree. So for instance, given the following binary tree, we'd get [2, 5, 7] if the method grabbed the correct means. <img src="https://storage.googleapis.com/a...

3. CHALLENGE

Max Per Level

Given a binary tree, write a method to return an array containing the largest (by value) node values at each level. In other words, we're looking for the max per level. So for instance, given the following binar...

4. CHALLENGE

Traverse in a Zig Zag

Here's a fun one: can you write a method to return the nodes of a binary tree in a zig zag level order? It should return a multi-dimensional array of...

5. CHALLENGE

Two Stack Queue

Building a Queue with Two Stacks: A Challenge in Engineering Elegance The Queue with Stacks In this challenge, your task is to implement a queue using two stacks. While many programming languages offer queues through arrays or lists, we're restricting our approach to only utilize stacks. The end result should b...

6. CHALLENGE

Climbing the Word Ladder

Imagine two words placed at the two ends of a ladder. Can you reach one word from another using intermediate words as rings of the ladder? Each successive or transition word has a difference of only one letter from its predecessor, and must belong to a provided dictionary. Can we achieve this using the shortest possible number of r...

7. CHALLENGE

The Two Coloring Graph Problem

Question Given a graph, can you use two colors to color each node of the graph, such that no two adjacent nodes have the same color? The Problem The graph coloring problem is a well-known problem in computer science. It requires coloring different node...

Cheat Sheet

  • Quick summary: a sequential collection where elements are added at one end and removed from the other end.
  • Important facts:
    • Modeled after a real-life queue: first come, first served.
    • First in, first out (FIFO) data structure.
    • Similar to a linked list, the first (last added) node is called the tail, and the last (next to be removed) node is called the head.
    • Two fundamental operations are enqueuing and dequeuing:
      • To enqueue, insert at the tail of the linked list.
      • To dequeue, remove at the head of the linked list.
    • Usually implemented on top of linked lists because they're optimized for insertion and deletion, which are used to enqueue and dequeue elements.
  • Pros:
    • Constant-time insertion and deletion.
  • Cons:
    • Access and search are O(n).
  • Notable uses:
    • CPU and disk scheduling, interrupt handling and buffering.
  • Time complexity (worst case):
    • Access: O(n)
    • Search: O(n)
    • Insertion (enqueuing): O(1)
    • Deletion (dequeuing): O(1)
  • See also: