Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Two Sum and Rotated Arrays
00:00
5 left

Two Sum and Rotated Arrays

MediumPython

Problem

For an mthree engineering assessment, implement two independent array operations in one function: find two indices whose values sum to a target, and locate a value in a rotated sorted array.

Formal Specification

Implement solve_queries(nums, target_sum, rotated_nums, search_target):

  1. In nums, return the indices of the two distinct elements whose values add to target_sum. Return indices in increasing order.
  2. In rotated_nums, return the index of search_target, or -1 if it is absent. rotated_nums was created by rotating a strictly increasing sorted array at an unknown pivot.

Return a dictionary with exactly these keys: two_sum and rotated_index. The input guarantees exactly one valid pair for the first operation.

Examples

Example 1

Input: nums = [2, 7, 11, 15], target_sum = 9,
       rotated_nums = [6, 7, 0, 1, 2, 4, 5], search_target = 0
Output: {"two_sum": [0, 1], "rotated_index": 2}

The values at indices 0 and 1 sum to 9, and 0 occurs at index 2 in the rotated array.

Example 2

Input: nums = [-3, 4, 3, 90], target_sum = 0,
       rotated_nums = [30, 40, 5, 10, 20], search_target = 25
Output: {"two_sum": [0, 2], "rotated_index": -1}

The pair -3 + 3 equals 0, while 25 does not appear in the rotated array.

Constraints

  • 2 <= len(nums) <= 10^5
  • 1 <= len(rotated_nums) <= 10^5
  • -10^9 <= nums[i], target_sum, rotated_nums[i], search_target <= 10^9
  • Values in rotated_nums are distinct.
  • rotated_nums is a rotation of a strictly increasing array.
  • Exactly one two-sum pair exists.

Constraints

  • 2 <= len(nums) <= 10^5
  • 1 <= len(rotated_nums) <= 10^5
  • -10^9 <= nums[i], target_sum, rotated_nums[i], search_target <= 10^9
  • Values in rotated_nums are distinct
  • rotated_nums is a rotation of a strictly increasing array
  • Exactly one two-sum pair exists

Function Signature

def solve_queries(nums, target_sum, rotated_nums, search_target):
Interviewer

Your question is Two Sum and Rotated Arrays. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.