Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Python String Splitting

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

Your question is Python String Splitting. 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

Collins Aerospace Pro Line Fusion diagnostic logs may contain repeated multi-character separators. Implement a function that separates a string immediately after the kth occurrence of a specified separator.

The separator itself must not appear in either returned part. Return the text before the kth occurrence and the remaining text after it. Occurrences are counted from left to right, starting at 1. If the separator occurs fewer than occurrence times, return [text, ""].

Formal Specification

Implement split_at_occurrence(text, separator, occurrence).

  • Input: text and separator are strings, and occurrence is a positive integer.
  • Output: a two-element list [left, right] containing strings.
  • The split occurs at the start of the requested occurrence, and the complete separator is removed.
  • The separator may contain multiple characters.

Constraints

  • 1 <= len(text) <= 10^5
  • 1 <= len(separator) <= len(text)
  • occurrence >= 1
  • Matching is case-sensitive
  • Overlapping matches are not counted separately

Function Signature

def split_at_occurrence(text, separator, occurrence):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output