Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Two Sum Coding Task

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

Your question is Two Sum Coding Task. 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

A CGI Nederland service workflow receives an unsorted list of integer identifiers and a target value. Implement a function that returns the indices of two distinct elements whose values add up to the target.

Formal Specification

Given an integer array nums and an integer target, return a list containing the two indices i and j such that nums[i] + nums[j] == target. Return the indices in ascending order of discovery. Each array element may be used at most once. Every valid input contains exactly one solution.

The function must return a Python list of two integers. Design an efficient solution suitable for large arrays.

Constraints

  • 2 <= len(nums) <= 100,000
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= target <= 10^9
  • Exactly one pair satisfies the target sum
  • The same element cannot be selected 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