FastPrepSort One Displaced Element
Problems/Sort One Displaced Element

Sort One Displaced Element

Glean logoGlean● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report asks for an O(n) sort when at most one element is out of place. It does not define that condition, how duplicates work, or what the function returns. Our version is about 82% the same as the reported question.

You are given an integer array values. It was formed from some nondecreasing array by removing at most one element and inserting that same element at any position.

Return the values in nondecreasing order. Your algorithm must run in O(n) time. Return a new array; do not modify values.

Function

sortOneDisplaced(values: int[]) → int[]

Examples

Example 1

values = [1,2,6,3,4,5,7]return = [1,2,3,4,5,6,7]

The value 6 is the only displaced element. Removing it and inserting it between 5 and 7 restores nondecreasing order.

Example 2

values = [1,4,2,3,4,5]return = [1,2,3,4,4,5]

Duplicate values are allowed. The first 4 can be moved beside the second 4.

Example 3

values = [1,2,3,3]return = [1,2,3,3]

The input is already nondecreasing, so the returned array has the same values.

Constraints

  • 1 <= values.length <= 200000
  • Every element is a 32-bit signed integer.
  • There exists a nondecreasing array from which values can be obtained by moving at most one element.

More Glean Phone Screen questions

  • Validate a Chess Bishop, Rook, or Queen MovePHONE SCREEN · Seen Nov 2026
  • Apply a 2048 Board MovePHONE SCREEN · Seen Oct 2026
  • Document Indexer Scheduling MetricsPHONE SCREEN · Seen Oct 2026
  • Group Transitive Synonyms by Four-Token ContextPHONE SCREEN · Seen Aug 2026

Similar questions at other companies

  • Top Ten Largest FilesGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Valid Triangle NumberBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Implement QuicksortByteDance · PHONE SCREENByteDance interview questions
  • Rank Word-Translation Hints from Sentence TranslationsDuolingo · PHONE SCREEN · ONSITE INTERVIEWDuolingo interview questions
  • Top K Frequent ElementsAmazon · OA · PHONE SCREEN · ONSITE INTERVIEW
  • Fair Capped Money DistributionGusto · PHONE SCREEN · ONSITE INTERVIEWGusto interview questions
  • Merge IntervalsApple · OA · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
  • Merge Sorted Squares of Two ArraysWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
  • Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
  • Filter, Sort, and Deduplicate Scheduled TasksRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
All Glean interview questionsSee Glean hiring insights
Saved
class Solution {
    public int[] sortOneDisplaced(int[] values) {
        // write your code here
    }
}
Checking account…