PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/xAI

Find kth element and sliding-window kth in stream

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of order statistics and selection in arrays as well as sliding-window stream processing, including maintaining kth elements with bounded-value frequency or bucket counts.

  • hard
  • xAI
  • Coding & Algorithms
  • Software Engineer

Find kth element and sliding-window kth in stream

Company: xAI

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

You are given two related tasks. ## Task 1: Kth element in an array Given an integer array `nums` of length `n` and an integer `k` (1-indexed), return the **k-th smallest** element in `nums`. - **Input:** `nums: int[]`, `k: int` - **Output:** `int` (the k-th smallest value) - **Constraints (typical interview):** - `1 <= n <= 2e5` - `1 <= k <= n` - Values may be large and can contain duplicates. Clarify with the interviewer if they mean k-th smallest vs k-th largest; assume k-th smallest unless stated otherwise. ## Task 2 (Follow-up): Kth element per time window in a large data stream You receive a stream of events `(timestamp, value)` in **non-decreasing** timestamp order. For a fixed window length `W` (time-based window), for each new event at time `t` you must output the **k-th smallest** `value` among all events with timestamps in the inclusive window: - `t - W < timestamp <= t` ### Requirements - The stream can be very large; you cannot store all past events. - Memory is limited; you should store only what is necessary for the current window. - Assume `value` falls within a known bounded range `[MIN, MAX]` so you can use a **bucket/count array** (frequency table) instead of storing all values. ### Output For each incoming event, output the current window’s k-th smallest value (or specify behavior if the window has fewer than `k` items; e.g., output `null`/`-1`/no output). ### Example If `W=5`, `k=2`, and events are: - `(1, 10)`, `(3, 7)`, `(6, 8)` Then at `t=6`, the window contains timestamps `(3,7)` and `(6,8)` (since `6-5=1`, and we take `timestamp > 1`), so the 2nd smallest is `8`.

Quick Answer: This question evaluates understanding of order statistics and selection in arrays as well as sliding-window stream processing, including maintaining kth elements with bounded-value frequency or bucket counts.

Related Interview Questions

  • Flatten and unflatten nested Python structures - xAI (nan)
  • Compute dasher pay from order events - xAI (nan)
  • Compute total active time per Twitter Space - xAI (medium)
  • Design a Recoverable Iterator - xAI (medium)
  • Implement Distributed Matrix Multiplication - xAI (hard)
xAI logo
xAI
Jan 22, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
7
0
Loading...

You are given two related tasks.

Task 1: Kth element in an array

Given an integer array nums of length n and an integer k (1-indexed), return the k-th smallest element in nums.

  • Input: nums: int[] , k: int
  • Output: int (the k-th smallest value)
  • Constraints (typical interview):
    • 1 <= n <= 2e5
    • 1 <= k <= n
    • Values may be large and can contain duplicates.

Clarify with the interviewer if they mean k-th smallest vs k-th largest; assume k-th smallest unless stated otherwise.

Task 2 (Follow-up): Kth element per time window in a large data stream

You receive a stream of events (timestamp, value) in non-decreasing timestamp order. For a fixed window length W (time-based window), for each new event at time t you must output the k-th smallest value among all events with timestamps in the inclusive window:

  • t - W < timestamp <= t

Requirements

  • The stream can be very large; you cannot store all past events.
  • Memory is limited; you should store only what is necessary for the current window.
  • Assume value falls within a known bounded range [MIN, MAX] so you can use a bucket/count array (frequency table) instead of storing all values.

Output

For each incoming event, output the current window’s k-th smallest value (or specify behavior if the window has fewer than k items; e.g., output null/-1/no output).

Example

If W=5, k=2, and events are:

  • (1, 10) , (3, 7) , (6, 8)

Then at t=6, the window contains timestamps (3,7) and (6,8) (since 6-5=1, and we take timestamp > 1), so the 2nd smallest is 8.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More xAI•More Software Engineer•xAI Software Engineer•xAI Coding & Algorithms•Software Engineer Coding & Algorithms
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.