Challenge: Reversing First k Elements of Queue

Can you reverse first "k" elements in a given queue? A solution is placed in the "solution" section for your help, but we would suggest you to solve it on your own first.

Problem Statement

Implement the function myQueue reverseK(myQueue queue, int k) which takes a queue and a number k as input and reverses the first k elements of the queue. An illustration is also provided for your understanding.


A queue and an integer k such that 0 \leq k\leq size of queue.


Queue with 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.