Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Two Sum and Variants

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

Your question is Two Sum and Variants. 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 Delivery Hero India client processes compact arrays and encoded values. Implement one function that performs three independent analyses efficiently:

  1. Two Sum: Return the indices of two distinct elements in nums whose values add to target. Return the first pair found while scanning from left to right, or [] if no pair exists.
  2. Binary Sum: Add two non-negative binary strings binary_a and binary_b without converting either full string to an integer. Return the canonical binary result without leading zeroes, except that zero is represented as "0".
  3. First Duplicate: Scan values from left to right and return the first value whose second occurrence appears earliest. Return None if every value is unique.

Return a dictionary with keys two_sum, binary_sum, and first_duplicate.

Formal Specification

nums is a list of integers, target is an integer, binary_a and binary_b are binary strings, and values is a list of hashable values. The result is a dictionary containing a list of indices, a binary string, and either a value or None.

Constraints

  • 0 <= len(nums), len(values) <= 10^5
  • -10^9 <= nums[i], target <= 10^9
  • Each binary string contains only '0' and '1'
  • The length of each binary string is at most 10^5
  • Values in values are hashable
  • Do not convert binary strings to integers

Function Signature

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