Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rotate a String With Constraints

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

Your question is Rotate a String With Constraints. 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

Datavant processing utilities often validate equivalent identifiers and reorder event data before downstream handling. Implement a rotation check using a dedicated string-rotation helper, then implement an array item move operation.

Write these functions:

  1. rotate_string(value, shift): return value left-rotated by shift positions. Normalize shifts larger than the string length.
  2. is_rotation(first, second): return True if second can be produced by rotating first any number of positions. This function must call rotate_string; do not solve the check with only a concatenation expression or a single inline function.
  3. move_index(values, from_index, to_index): remove the item currently at from_index and insert it at to_index in the resulting array. Return the modified array. The input array may be copied or modified in place, but the returned ordering must be correct.

The required interview entry point is validate_and_move, which returns a dictionary containing the rotation result and moved array.

Formal Specification

first and second are strings. values is a list of integers, and both indices are valid. Return {"is_rotation": bool, "array": list[int]}.

Constraints

  • 0 <= len(first), len(second) <= 2,000
  • 0 <= len(values) <= 1,000
  • Indices are valid whenever values is non-empty
  • to_index is the final index after the source item is removed
  • Strings contain arbitrary characters and are case-sensitive

Function Signature

def validate_and_move(first, second, values, from_index, to_index):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output