PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates array manipulation, ordering by derived keys (squares/absolute values), order-statistics reasoning, and algorithmic complexity analysis within the Coding & Algorithms domain.

  • Medium
  • Uber
  • Coding & Algorithms
  • Software Engineer

Sort by squares and find k-th smallest

Company: Uber

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Given a nondecreasing sorted array of integers nums, do the following: 1) Reorder the original elements by increasing square value (equivalently, by absolute value). For elements whose squares are equal, any order is acceptable. Target time complexity strictly better than O(N log N); aim for O(N) time. Example: nums = [-3, -2, 0, 1, 2, 5] -> [0, 1, -2, 2, -3, 5]. 2) Follow-up: Given the same nums and an integer k (1-indexed), return the k-th element in the order induced by sorting by square value, without fully materializing that order. You must not explicitly sort by squares; target O(log N) time. Clearly state your algorithm, complexity, and any assumptions.

Quick Answer: This question evaluates array manipulation, ordering by derived keys (squares/absolute values), order-statistics reasoning, and algorithmic complexity analysis within the Coding & Algorithms domain.

Sort Values by Increasing Square

Given a sorted array, return original values ordered by increasing square value.

Constraints

  • nums is sorted non-decreasing

Examples

Input: ([-3, -2, 0, 1, 2, 5],)

Expected Output: [0, 1, -2, 2, -3, 5]

Explanation: Prompt example up to tie order.

Input: ([-4, -1, 0, 3, 10],)

Expected Output: [0, -1, 3, -4, 10]

Explanation: Mixed signs.

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

Expected Output: [-1, -2]

Explanation: All negative.

Hints

  1. Compare absolute values from both ends and fill from the back/front.

k-th Value by Increasing Square

Return the k-th original value in the order induced by sorting by square value.

Constraints

  • k is 1-indexed

Examples

Input: ([-3, -2, 0, 1, 2, 5], 1)

Expected Output: 0

Explanation: Smallest square.

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

Expected Output: 2

Explanation: Tie order follows the deterministic two-pointer order.

Input: ((-5, -4, -1), 2)

Expected Output: -4

Explanation: All negative input as tuple.

Hints

  1. This exact-match version uses the same deterministic two-pointer order as the full ordering problem.
Last updated: Jun 27, 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
  • AI Coding 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

  • Thread-Safe Token-Bucket Rate Limiter - Uber
  • Quadtree for 2D Geospatial Points - Uber
  • Group Anagrams - Uber
  • Deep Equality of Two Records - Uber (medium)
  • Shortest Path in a Grid with Blocked Cells - Uber (medium)