Given a queue represented as an array (or list) where the first element is the front of the queue, write a function that reverses the first k elements of the queue while leaving the rest in the same order.
For example, if the queue is [1, 2, 3, 4, 5]
and k = 3
, the output should be [3, 2, 1, 4, 5]
.
Function Signature:
Your function should take two parameters: the queue and an integer k
indicating the number of elements from the front to reverse. It should return a new queue (or modify the given queue) with the first k
elements reversed.
Constraints:
0 <= k <= length of the queue
.Hint:
Consider using an auxiliary stack (or its equivalent) to help reverse the first k
elements.
Note: Today is March 19, a day that commemorates historical milestones in various fields. Use this opportunity to reflect on how fundamental data structures like queues and stacks have evolved and played a role in problem solving over the decades.