PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches
|Home/Coding & Algorithms/Uber

Check if each prefix forms 1..k permutation

Last updated: Mar 29, 2026

Quick Overview

This question evaluates algorithmic thinking and understanding of permutations and prefix invariants, measuring the ability to recognize when the first k elements of a permutation can form the set {1..k}.

  • hard
  • Uber
  • Coding & Algorithms
  • Software Engineer

Check if each prefix forms 1..k permutation

Company: Uber

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Take-home Project

You are given an integer array `arr` of length `n` that is a permutation of the numbers `1..n` (each number appears exactly once), but in arbitrary order. For each `k` from `1` to `n`, consider the prefix subarray `arr[0..k-1]` (the first `k` elements). Determine whether these `k` elements can be rearranged to become exactly `[1, 2, ..., k]`. Return a binary array `res` of length `n` where: - `res[k-1] = 1` if `arr[0..k-1]` can be rearranged into `[1..k]` - `res[k-1] = 0` otherwise ### Example Input: `arr = [5, 3, 1, 2, 4]` Prefixes: - `k=1`: `[5]` cannot be `[1]` → `0` - `k=2`: `[5,3]` cannot be `[1,2]` → `0` - ... (If instead using the intended example semantics that output is all ones, clarify whether the window is a *sliding window* of size `k` over the array, or specifically the *prefix* of length `k`.) ### Constraints - `1 <= n <= 2e5` (or similar) - `arr` is a permutation of `1..n` Implement an efficient algorithm (better than checking/sorting each prefix independently).

Quick Answer: This question evaluates algorithmic thinking and understanding of permutations and prefix invariants, measuring the ability to recognize when the first k elements of a permutation can form the set {1..k}.

Related Interview Questions

  • Implement stream queries and bounded-difference subarrays - Uber (medium)
  • Implement Minesweeper and Word Search - Uber (medium)
  • Implement Store Autocomplete - Uber (medium)
  • Simulate a Rank-Based Tournament - Uber (medium)
  • Implement Cache Eviction And Seat Assignment - Uber (medium)
Uber logo
Uber
Feb 11, 2026, 12:00 AM
Software Engineer
Take-home Project
Coding & Algorithms
14
0

You are given an integer array arr of length n that is a permutation of the numbers 1..n (each number appears exactly once), but in arbitrary order.

For each k from 1 to n, consider the prefix subarray arr[0..k-1] (the first k elements). Determine whether these k elements can be rearranged to become exactly [1, 2, ..., k].

Return a binary array res of length n where:

  • res[k-1] = 1 if arr[0..k-1] can be rearranged into [1..k]
  • res[k-1] = 0 otherwise

Example

Input: arr = [5, 3, 1, 2, 4]

Prefixes:

  • k=1 : [5] cannot be [1] → 0
  • k=2 : [5,3] cannot be [1,2] → 0
  • ...

(If instead using the intended example semantics that output is all ones, clarify whether the window is a sliding window of size k over the array, or specifically the prefix of length k.)

Constraints

  • 1 <= n <= 2e5 (or similar)
  • arr is a permutation of 1..n

Implement an efficient algorithm (better than checking/sorting each prefix independently).

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Uber•More Software Engineer•Uber Software Engineer•Uber Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

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