Challenge: Reversing the First k Elements of a Queue
Can you reverse the first "k" elements in a given queue? A solution is placed in the "solution" section to help you, but we would suggest you try to solve it on your own first.
We'll cover the following
Problem Statement
In this problem, you have to implement the void reverseK(Queue<V> queue, int k)
method; this will take a queue and any number (k
) as input, and reverse the first k
elements of the queue. An illustration is also provided for your understanding.
Method Prototype
void reverseK(Queue<V> queue, int k)
Output
An array with the first “k” elements reversed.
Sample Input
Queue = {1,2,3,4,5,6,7,8,9,10}
k = 5
Sample Output
result = {5,4,3,2,1,6,7,8,9,10}
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.