Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Two Sum and Linked List Reversal

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

Your question is Two Sum and Linked List Reversal. 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

DoorDash routing utilities need two independent algorithms: identify two delivery indices whose priorities reach a target, then reverse a linked list of delivery stops. Implement both operations in one function.

Given an integer array nums, an integer target, and the head of a singly linked list, return the indices of two values that sum to target and the new head after reversing the linked list.

For JSON compatibility, each linked-list node is represented as a dictionary with keys val and next. The next value is another node dictionary or null.

Formal Specification

Implement two_sum_and_reverse(nums, target, head).

  • nums is a list of integers.
  • target is an integer.
  • head is a linked-list node dictionary or null.
  • Return a dictionary with two_sum, a two-element list of indices in ascending order, and reversed_head, the new linked-list head.
  • Each input has exactly one valid Two Sum pair.
  • Reverse the list by changing node pointers, not by copying values into a new list.

Constraints

  • 2 <= len(nums) <= 10^5
  • -10^9 <= nums[i], target <= 10^9
  • 0 <= number of linked-list nodes <= 10^5
  • Each linked-list node has exactly one val and one next field
  • Exactly one valid Two Sum pair exists

Function Signature

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