Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Linked List and String Reversal Patterns

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

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

Red Hat test tooling may need to transform textual data and linked-list representations during validation. Implement one function that performs three independent operations: reverse a string through a specified index, reverse a singly linked list, and merge two sorted singly linked lists.

Formal Specification

Implement solve(s, k, reverse_head, list1, list2):

  1. Reverse the characters in s from index 0 through index k, inclusive. Leave the remaining suffix unchanged.
  2. Reverse reverse_head in place and return the new head.
  3. Merge list1 and list2, which are sorted in nondecreasing order, into one sorted linked list. Reuse the existing nodes rather than creating replacement value nodes.

Each linked list is represented as a nested JSON-compatible object with integer field value and nullable field next. The function returns a dictionary containing reversed_string, reversed_list, and merged_list, each in the same serialized linked-list format.

Constraints

  • 1 <= len(s) <= 10^5
  • 0 <= k < len(s)
  • Each linked list contains at most 10^5 nodes
  • Node values are integers in [-10^9, 10^9]
  • list1 and list2 are sorted in nondecreasing order
  • All linked lists are acyclic

Function Signature

def solve(s, k, reverse_head, list1, list2):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output