Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Two Sum-Style Coding Practice

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

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

During validation of Zscaler Internet Access policy rules, you receive an unsorted list of integer rule priorities and a target priority total. Find two distinct entries whose priorities add up to the target.

Return their zero-based indices as [i, j], where i < j. If multiple valid pairs exist, return the pair with the smallest second index. If no pair exists, return [-1, -1].

Use a hash map to avoid checking every possible pair. Explain why the map must be checked before inserting the current value.

Formal Specification

Implement find_priority_pair(priorities, target).

  • Input: priorities, a list of integers, and target, an integer.
  • Output: a two-element list containing the indices of a valid pair, or [-1, -1] when no pair exists.
  • Each array entry may be used at most once.
  • The input list must not be modified.

Constraints

  • 2 <= len(priorities) <= 100,000
  • -10^9 <= priorities[i] <= 10^9
  • -10^9 <= target <= 10^9
  • The input list must not be modified
  • Return the first valid pair encountered by increasing right-hand index

Function Signature

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