Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Optimized Array Solution With Trade-offs

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Optimized Array Solution With Trade-offs. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Given an array of integers, write an optimized solution to find a specific target condition and explain your trade-offs against a brute-force approach.

For this task, the target condition is finding two distinct elements whose values sum to target. Return their zero-based indices, or [] if no pair exists. Explain why the optimized approach improves on checking every pair.

Function: def two_sum(nums, target):

Input: an integer array and integer target. Output: a list containing the two indices in ascending order, or an empty list.

Constraints

  • 0 <= nums.length <= 10000
  • -1000000000 <= nums[i] <= 1000000000
  • -2000000000 <= target <= 2000000000
  • Return any valid pair if multiple pairs exist
  • Do not use the same array element twice

Function Signature

def two_sum(nums, target):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output