PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Meta

Implement randomized Quickselect without k-shift bug

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of randomized Quickselect and selection algorithms, including in-place partition semantics for the k-th largest element, careful index and k-update reasoning to prevent k-shift/off-by-one bugs, handling duplicates and adversarial pivots, and analysis of expected versus worst-case time and auxiliary space.

  • Medium
  • Meta
  • Coding & Algorithms
  • Data Scientist

Implement randomized Quickselect without k-shift bug

Company: Meta

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Implement randomized Quickselect to return the k-th largest element (1-based k, 1 ≤ k ≤ n) from an unsorted integer array. Use an in-place partition that places elements greater than the pivot to the left and less than the pivot to the right, returning the pivot’s final index p. Let rank = p - left + 1 (the number of elements ≥ pivot in the current subarray). If k == rank, return A[p]; if k < rank, recurse on [left..p-1]; otherwise recurse on [p+1..right] with k' = k - rank. Prove that this k-update rule prevents negative positions and off-by-one errors. Handle duplicates, very small arrays, and adversarial inputs by choosing a random pivot each iteration. Analyze expected time O(n) and worst-case time O(n^2), and specify auxiliary space. Finally, show how to adapt the same routine to find the k-th smallest and explain the difference from Quicksort.

Quick Answer: This question evaluates understanding of randomized Quickselect and selection algorithms, including in-place partition semantics for the k-th largest element, careful index and k-update reasoning to prevent k-shift/off-by-one bugs, handling duplicates and adversarial pivots, and analysis of expected versus worst-case time and auxiliary space.

Related Interview Questions

  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)
  • Solve a Key-Door Corridor Maze - Meta (medium)
  • Solve Array Merge and Parentheses Cleanup - Meta (medium)
Meta logo
Meta
Oct 13, 2025, 9:49 PM
Data Scientist
Technical Screen
Coding & Algorithms
3
0

Implement randomized Quickselect to return the k-th largest element (1-based k, 1 ≤ k ≤ n) from an unsorted integer array. Use an in-place partition that places elements greater than the pivot to the left and less than the pivot to the right, returning the pivot’s final index p. Let rank = p - left + 1 (the number of elements ≥ pivot in the current subarray). If k == rank, return A[p]; if k < rank, recurse on [left..p-1]; otherwise recurse on [p+1..right] with k' = k - rank. Prove that this k-update rule prevents negative positions and off-by-one errors. Handle duplicates, very small arrays, and adversarial inputs by choosing a random pivot each iteration. Analyze expected time O(n) and worst-case time O(n^2), and specify auxiliary space. Finally, show how to adapt the same routine to find the k-th smallest and explain the difference from Quicksort.

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Meta•More Data Scientist•Meta Data Scientist•Meta Coding & Algorithms•Data Scientist Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ 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.