AlgoLounge

Implement Queue

easy

Implement a queue data structure with the following operations:

  • enqueue(val): Add an element to the end of the queue
  • dequeue(): Remove and return the front element from the queue
  • front(): Return the front element without removing it
  • empty(): Return whether the queue is empty

You can choose to implement the queue using either:

  • Linked List - Each node points to the next node
  • Circular Array - Fixed-size array with wrapping

Examples

Example 1

Input:  ["Queue", "enqueue", "enqueue", "front", "dequeue", "empty"]

[[], [1], [2], [], [], []] Output: [null, null, null, 1, 1, false]

Example 2

Input:  ["Queue", "enqueue", "dequeue", "empty"]

[[], [10], [], []] Output: [null, null, 10, true]

Constraints

  • 1 <= val <= 10^9
  • At most 10^4 calls will be made to each function
  • dequeue and front will only be called on non-empty queues

How the tests work: each test is a sequence of calls. operations[i] is the method called with arguments[i]; the first entry constructs the class. The expected output lists what every call returns — null for the constructor and for methods that return nothing.

TypeScript
Loading editor…
List of test cases (10)
Case 1
operations = ["Queue","enqueue","enqueue","front","dequeue","empty"]
arguments = [[],[1],[2],[],[],[]]
Case 2
operations = ["Queue","enqueue","dequeue","empty"]
arguments = [[],[10],[],[]]
Case 3
operations = ["Queue","enqueue","enqueue","enqueue","dequeue","dequeue","dequeue","empty"]
arguments = [[],["hello"],["world"],["test"],[],[],[],[]]
Case 4
operations = ["Queue","enqueue","front","front","dequeue","empty"]
arguments = [[],[5],[],[],[],[]]
Case 5
operations = ["Queue","empty","enqueue","empty","enqueue","front"]
arguments = [[],[],[100],[],[200],[]]
Case 6
operations = ["Queue","enqueue","enqueue","enqueue","enqueue","enqueue","dequeue","dequeue","dequeue","dequeue","dequeue","empty"]
arguments = [[],["a"],["b"],["c"],["d"],["e"],[],[],[],[],[],[]]
Case 7
operations = ["Queue","enqueue","enqueue","dequeue","enqueue","dequeue","dequeue","empty"]
arguments = [[],[7],[8],[],[9],[],[],[]]
Case 8
operations = ["Queue","enqueue","front","enqueue","front","enqueue","front"]
arguments = [[],["x"],[],["y"],[],["z"],[]]
Case 9
operations = ["Queue","enqueue","enqueue","enqueue","front","dequeue","front","dequeue","front"]
arguments = [[],[10],[20],[30],[],[],[],[],[]]
Case 10
operations = ["Queue","enqueue","empty","dequeue","empty","enqueue","enqueue","dequeue","empty"]
arguments = [[],[42],[],[],[],[50],[60],[],[]]