Prepin
Log in

Identify the smallest element that is absent from a sorted array.

Approach / Explanation

In this question, the interviewer is requesting that we create a program to identify the smallest missing number in a provided sorted array of integers. Here are some key points to keep in mind as you approach this question: You will be working with a sorted array, which will simplify several aspects of the problem. The array will consist of integers, including both positive and negative values. Your task is to determine the smallest integer that is absent from the given array. To address this problem, one effective method is to utilize a binary search algorithm, taking advantage of the sorted nature of the array. However, since the goal is to find the smallest missing number, initiating the search from the left side will be a particularly efficient strategy.

Suggested Answer

In Python, a solution can be written as follows: def findSmallestMissing ( arr ) : for i in range ( len ( arr ) ) : if arr [ i ] > i + 1 : return i + 1 return len ( arr ) + 1 This code produces numbers from 1 up to the length of the array plus 1, and checks if the corresponding element in the array is greater than the current number. This process helps to identify any missing numbers in the array. If a missing number is found, it is returned immediately. If no missing numbers are detected after examining all elements, the function returns the length of the list plus 1, as this is the smallest positive integer that is greater than all elements in the array.

Alternative Answer

If you need to implement this in Java, here’s how it would appear: public int findSmallestMissing ( int [ ] array ) { for ( int i = 0 ; i < array . length ; i ++ ) { if ( array [ i ] > i + 1 ) { return i + 1 ; } } return array . length + 1 ; } Similar to the Python implementation, this Java method goes through the provided array. For each index i , it checks if the element at that index exceeds i+1 . If it does, then i+1 is identified as the smallest missing number. If no such number is located after checking all the elements, the method returns the length of the array plus one, which corresponds to the smallest positive integer that is larger than all the numbers in the array.

Question Details

Difficulty & Category

Medium
algorithms

Companies

Meta (Facebook)

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.