PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/IBM

Find minimum operations to make array sorted

Last updated: May 2, 2026

Quick Overview

This question evaluates a candidate's competence in array manipulation, algorithmic optimization, and reasoning about deterministic, operation-driven state transitions.

  • easy
  • IBM
  • Coding & Algorithms
  • Data Scientist

Find minimum operations to make array sorted

Company: IBM

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Take-home Project

## Problem You are given an integer array `a` of length `n`. Define **one operation** as follows: 1. Remove the first element `x = a[0]`. 2. Append `x` to the end of the array. 3. Starting from the end, repeatedly swap `x` leftward **while** it has a left neighbor and `x` is **smaller** than that left neighbor (i.e., while `a[i] < a[i-1]`). Stop when either: - `x` reaches index `0`, or - `a[i-1] <= a[i]`. After each operation, the array is updated deterministically. ### Task Return the **minimum number of operations** needed to make the array **non-decreasing** (i.e., for all `i`, `a[i] <= a[i+1]`). If it is **impossible** for the array to ever become non-decreasing under repeated operations, return `-1`. ### Function signature (example) Implement a function like: - `min_ops_to_nondecreasing(a: List[int]) -> int` ### Assumptions / constraints (for interview) - `1 <= n <= 2e5` - `a[i]` fits in 32-bit signed integer - Time complexity should be better than simulating full array states.

Quick Answer: This question evaluates a candidate's competence in array manipulation, algorithmic optimization, and reasoning about deterministic, operation-driven state transitions.

Related Interview Questions

  • Reverse last two characters with space - IBM (nan)
  • Compute minimum rooms for meeting schedule - IBM (nan)
  • Maximize palindromic strings after character swaps - IBM (medium)
  • Find minimum subarray length with k distinct integers - IBM (medium)
  • Implement valid 1-D convolution - IBM (Medium)
IBM logo
IBM
Oct 3, 2025, 12:00 AM
Data Scientist
Take-home Project
Coding & Algorithms
4
0

Problem

You are given an integer array a of length n. Define one operation as follows:

  1. Remove the first element x = a[0] .
  2. Append x to the end of the array.
  3. Starting from the end, repeatedly swap x leftward while it has a left neighbor and x is smaller than that left neighbor (i.e., while a[i] < a[i-1] ). Stop when either:
    • x reaches index 0 , or
    • a[i-1] <= a[i] .

After each operation, the array is updated deterministically.

Task

Return the minimum number of operations needed to make the array non-decreasing (i.e., for all i, a[i] <= a[i+1]). If it is impossible for the array to ever become non-decreasing under repeated operations, return -1.

Function signature (example)

Implement a function like:

  • min_ops_to_nondecreasing(a: List[int]) -> int

Assumptions / constraints (for interview)

  • 1 <= n <= 2e5
  • a[i] fits in 32-bit signed integer
  • Time complexity should be better than simulating full array states.

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More IBM•More Data Scientist•IBM Data Scientist•IBM 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.