Challenge: Reversing First k Elements of Queue
Can you reverse the first "k" elements in a given queue? A solution is placed in the "solution" section for your help, but we would suggest you solve it on your own first.
We'll cover the following
Problem Statement
Implement the function reverseK(queue, 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.
Input
A queue and an integer k
such that
0 k
size of the queue
Output
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.