Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Reverse Array and String Construction

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

Your question is Reverse Array and String Construction. 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

Cisco Restaurant + Bar receives an integer array representing display positions and a character inventory from an origin string. Reverse the array in place, then construct the lexicographically smallest string matching a target pattern, where each ? can be replaced by one unused character from the origin string. Fixed characters must remain unchanged and do not consume inventory.

Return a dictionary containing the reversed array and the constructed string. If the pattern cannot be completed, return None for the constructed value.

Formal Specification

Implement reverse_and_construct(nums, origin, target).

  • nums is a mutable list of integers. Modify this same list and return it under the reversed key.
  • origin is a lowercase English string whose characters form the available inventory.
  • target is a lowercase pattern containing letters and ? characters.
  • Return {"reversed": nums, "constructed": string_or_none}.

Replacement characters may be used at most once. To minimize the result lexicographically, assign the smallest available character to each ? from left to right.

Constraints

  • 0 <= len(nums), len(origin), len(target) <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • origin contains only lowercase English letters
  • target contains lowercase English letters and ? characters
  • Each ? consumes exactly one character from origin

Function Signature

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