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
4mo

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!
Select
Add answer anonymously...

Nagarro SDE interview questions & answers

A SDE was asked Q. Duplicate Subtrees Problem Statement Given a binary tree, return the root values...read more
A SDE was asked Q. Maximum Meetings Selection You are tasked with scheduling meetings in a single m...read more
A SDE was asked Q. Can you explain the concept of keys in database management systems?

Popular interview questions of SDE

A SDE was asked Q1. Duplicate Subtrees Problem Statement Given a binary tree, return the root values...read more
A SDE was asked Q2. Maximum Meetings Selection You are tasked with scheduling meetings in a single m...read more
A SDE was asked Q3. Can you explain the concept of keys in database management systems?
Nagarro SDE Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits