Implement Queue using Arrays

Stack / Queues Implementation Easy

Implement a First-In-First-Out (FIFO) queue using an array. The implemented queue should support the following operations: push, dequeue, pop, and isEmpty.


Implement the ArrayQueue class:


void push(int x): Adds element x to the end of the queue.

int pop(): Removes and returns the front element of the queue.

int peek(): Returns the front element of the queue without removing it.

boolean isEmpty(): Returns true if the queue is empty, false otherwise.

Examples:

Input:

["ArrayQueue", "push", "push", "peek", "pop", "isEmpty"]

[[], [5], [10], [], [], []]

Output: [null, null, null, 5, 5, false]

Explanation:

ArrayQueue queue = new ArrayQueue();

queue.push(5);

queue.push(10);

queue.peek(); // returns 5

queue.pop(); // returns 5

queue.isEmpty(); // returns false

Input:

["ArrayQueue", "isEmpty"]

[[]]

Output:[null, true]

Explanation:

ArrayQueue queue = new ArrayQueue();

queue.isEmpty(); // returns true

Input:

["ArrayQueue", "push", "pop", "isEmpty"]

[[], [1], [], []]

Constraints

  • 1 <= numbers of calls made <= 100
  • 1 <= x <= 100

Hints

  • Think of it as a line at a checkout counter where the first person in line gets served first.

Company Tags

Boston Consulting Group Nutanix Morgan Stanley Wayfair Pinterest PayPal Visa Medtronic HashiCorp Johnson & Johnson Snowflake Cloudflare Ernst & Young Siemens Healthineers Texas Instruments NVIDIA Riot Games Epic Systems American Express Chewy Robinhood Flipkart Bloomberg Rockstar Games Dropbox Google Microsoft Amazon Meta Apple Netflix Adobe TCS Cognizant Accenture Infosys Capgemini Wipro