Grokking Data Structures & Algorithms for Coding Interviews

0% completed

Solution: Reverse a Queue

Problem Statement

Given a queue containing integer elements, return the updated queue after reversing its elements.

Examples

Example 1

  • Input: queue = [1, 2, 3, 4, 5]
  • Expected Output: [5, 4, 3, 2, 1]
  • Explanation: The input queue elements are reversed.

Example 2

  • Input: queue = [10, 20, 30, 40, 50]
  • Expected Output: [50, 40, 30, 20, 10]
  • Explanation: The input queue elements are reversed.

Example 3

  • Input: queue = [5, 7, 12, 2, 4, 5]
  • Expected Output: [5, 4, 2, 12, 7, 5]

.....

.....

.....

Like the course? Get enrolled and start learning!
O

odhran.19

· 2 years ago

I want some clarity on what implementation of a Queue we should expect to see in the problem set. In the first problem the queue is implemented as a list, in Python.

It was not clear that the front of the 'queue' should be the 0 index of the list?

Why show a Queue that uses Node and .next, .enqueue, .dequeue functions if these are not used?

In a real interview leetcode/hackerrank/coderpad what approach should we use?

Show 2 replies
S

SK

· 3 years ago

Why are we using peek() instead of remove() and pop() ?

Show 1 reply
D

Daven L

· 2 years ago

Is it possible to get more detailed feedback on these queue questions?

I am getting Unknown Error - Check your Input

I don't know what type queue is, so I tried printing it to see if it is of type Node or if it is a list in order to begin working through this problem.

Show 2 replies
Leopoldo Hernandez

Leopoldo Hernandez

· 2 years ago

However... this doesn't have them and uses arrays instead. What is the point of teaching stacks and queues?

Show 1 reply
Moshe Peretz

Moshe Peretz

· 2 years ago

i wrote "while(queue.isEmpty())" but its show me runtime erorr that isEmpty is not function

Show 4 replies
E

erickchhuang

· 10 months ago

<p>Previous sections introduced dequeue(), enqueue(), etc. However, it isn't being used as part of the implementation for the practice problem. This part is unclear and super confusing. Why introduce something we don't use? I initially thought I had to create a Queue class to solve this question.</p>