PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates algorithmic problem-solving skills, specifically understanding sorting under constrained inputs and rigorous time and space complexity analysis.

  • hard
  • Citadel
  • Coding & Algorithms
  • Data Scientist

Sort a nearly sorted array

Company: Citadel

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

Given an array of length \(n\) in which every element is at most \(k\) positions away from its location in the fully sorted order, design an algorithm to return the sorted array. Aim for a solution that is more efficient than \(O(n \log n)\), and state the time and space complexity.

Quick Answer: This question evaluates algorithmic problem-solving skills, specifically understanding sorting under constrained inputs and rigorous time and space complexity analysis.

Given an integer array nums of length n where every element is at most k positions away from its position in the fully sorted array, return the array sorted in nondecreasing order. Design an algorithm that takes advantage of the nearly sorted property and is more efficient than sorting the entire array with a general O(n log n) algorithm when k is small.

Constraints

  • 0 <= len(nums) <= 100000
  • 0 <= k <= 100000
  • -1000000000 <= nums[i] <= 1000000000
  • nums is guaranteed to be k-nearly sorted

Examples

Input: ([6, 5, 3, 2, 8, 10, 9], 3)

Expected Output: [2, 3, 5, 6, 8, 9, 10]

Explanation: Each value is at most 3 positions away from its sorted position. Sorting the array gives the expected result.

Input: ([-1, -3, -2, 2, 1, 4], 2)

Expected Output: [-3, -2, -1, 1, 2, 4]

Explanation: The array contains negative numbers and is 2-nearly sorted. The sorted order is [-3, -2, -1, 1, 2, 4].

Input: ([], 4)

Expected Output: []

Explanation: An empty array is already sorted.

Input: ([1, 2, 3, 4], 0)

Expected Output: [1, 2, 3, 4]

Explanation: When k is 0, every element is already exactly in its sorted position.

Input: ([2, 1, 1, 3, 3], 2)

Expected Output: [1, 1, 2, 3, 3]

Explanation: Duplicates should be handled correctly while producing nondecreasing order.

Input: ([42], 5)

Expected Output: [42]

Explanation: A single-element array is sorted regardless of k.

Hints

  1. At any point, the next smallest element must be among the next k + 1 elements that have not yet been placed.
  2. Use a data structure that can efficiently return and remove the smallest candidate.
Last updated: Jun 20, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Sort a Nearly Sorted Array - Citadel (hard)
  • Implement a single-producer multi-consumer ring buffer - Citadel (medium)
  • Compute BBO and NBBO from order data - Citadel (medium)
  • Simulate 2048 and pack board into uint64 - Citadel (medium)
  • Implement task queue with insert, delete, execute - Citadel (medium)