Asked inNagarro,SDE

Sort a "K" Sorted Doubly Linked List

Given a doubly-linked list with N nodes, where each node’s position deviates at most K positions from its position in the sorted list, your task is to sort this given doubly linked list.

Example:

Let us consider K is 3. An element at position 4 in the sorted doubly linked list can be at positions 1, 2, 3, 4, 5, 6, 7 in the given linked list because the absolute difference of all these indices with 4 is at most 3.

Input:

The input starts with an integer 'T', representing the number of test cases.
For each test case:
- A line containing integer 'K'.
- A second line with the elements of the doubly linked list separated by spaces and terminated by -1. -1 is not part of the list.

Output:

For each test case, output the sorted elements of the linked list in a single line, separated by spaces, and terminated by -1.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 10000
  • 1 <= K < N
Note:

All elements in the doubly linked list are distinct. A doubly linked list can be traversed in both directions, forward and backward. Implement the function as required. Printing is handled separately.

AnswerBot
4d

Sort a doubly linked list where each node's position deviates at most K positions from its position in the sorted list.

  • Iterate through the doubly linked list and maintain a min-heap of size K+1 to kee...read more

Help your peers!
Add answer anonymously...
Nagarro SDE Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter