Problem Submissions Solution

Reverse First K elements of Queue

Difficulty: Medium

Acceptance: %

Points: 30.00

Given an integer K and a queue of integers, the task is to reverse the order of the first K elements in the queue, while keeping the remaining elements in their original relative order.

The following standard queue operations are allowed:

  • enqueue(x): Adds an element x to the rear of the queue.
  • dequeue(): Removes an element from the front of the queue.
  • size(): Returns the current number of elements in the queue.
  • front(): Retrieves the element at the front of the queue.

Complete the given function modifyQueue(), which takes a queue and an integer K as input parameters, and returns the modified queue. The driver code will handle the printing of the queue automatically.

Topics

Companies

Articles

Examples:

Expected Time Complexity: O(N)

Expected Auxiliary Space: O(K)

Constraints:
  • 1 <= K <= N <= 10^5
Companies:
Amazon Visa Goldman Sachs
Topics:
Queue
Locked Content
Access Restricted: Please Login to access the code editor and test cases.