Prepin
Log in

Check if an array containing numbers from 1 to n has any duplicates, using constant time and space.

Approach / Explanation

The interviewer is requesting an algorithm to check whether an array, containing values from 1 to n, has any duplicate entries. There are two additional constraints that complicate this task. The first constraint requires the solution to operate with constant time complexity, meaning that the algorithm's performance should remain unchanged regardless of the array's size. The second constraint stipulates that the solution must utilize constant space complexity, indicating that the memory utilized should not vary with the array's size. To effectively address this question, it is important to keep in mind the following key points: The algorithm must execute in constant time; thus, methods involving loops, recursive calls, or any operations that check each element are not suitable solutions. The algorithm must also function in constant space; therefore, creating any data structures to hold elements or copies of the array is not a recommended approach.

Suggested Answer

In an array of size n where elements range from 1 to n, there should ideally be no duplicates since each element should appear exactly once. However, this is just the ideal scenario; in practice, the array may contain duplicates. One method to identify duplicates is to compute the sum of all elements in the array and compare it to the sum of the first n natural numbers, which can be calculated using the formula (n (n + 1)) / 2. If the sum of the array's elements exceeds the sum of the first n natural numbers, it indicates the presence of a duplicate. Nevertheless, this method is ineffective if there are multiple duplicates or if some numbers are missing from the array. There is no straightforward solution to this problem that can achieve O(1) time and O(1) space due to the constraints of time and space complexity.

Alternative Answer

An alternative method involves utilizing mathematical principles, particularly those related to arithmetic series. You can determine the expected sum of an arithmetic series by using the formula n*(n+1)/2 . After obtaining this value, you should subtract the actual sum of the elements in the array (which entails summing the elements of the array). If there is one duplicate in the array and no numbers are missing between 1 and n, the result will reveal the duplicate number. If the outcome is zero, it indicates that no duplicates are present. It’s essential to understand that this method is effective only if there is a single duplicate and every number in the range from 1 to n is accounted for. If these criteria are not fulfilled, it will not be possible to resolve the problem within O(1) time and O(1) space constraints. This solution has limitations and does not address all possible edge cases, so it’s important to communicate this clearly to the interviewer.

Question Details

Difficulty & Category

Medium
algorithms

Companies

Microsoft

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

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