Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Optimize Duplicate Detection in Arrays

Easy
EasyCodingSearchingSortingGreedy

Problem

At Stripe, a service receives a list of integer event IDs and needs to quickly determine whether any ID appears more than once. A naive solution compares every pair of elements, but this is too slow for large inputs. Write an optimized algorithm to detect duplicates.

Formal Specification

Implement a function contains_duplicate(nums) that takes a list of integers nums and returns a boolean:

  • True if any value appears at least twice
  • False if all values are distinct

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • Input contains integers only
  • Target solution should improve on O(n^2) brute force

Function Signature

def contains_duplicate(nums):
Take this as a live interview session →

You are practicing as a guest. Sign up free to run your code against the sample data. Your draft stays right here.

Sign up freeI have an account
def solve(rows):
    counts = {}
    for row in rows:
        ...
    return result
Sign up to unlock solutions
Next questions
xAIOptimize Duplicate Pair DetectionMediumBae Systems UsaDetect Duplicates in ArrayEasyButtonFind Duplicate Values in ArrayEasy