Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Search and Split a String

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

Your question is Search and Split a String. 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

The Zappos Family catalog processes product labels that use a single character as a separator. Implement a custom split operation without calling Python's built-in str.split.

Given a string text, a one-character separator, and a nonnegative integer max_splits, return the pieces produced by scanning from left to right. Split at each separator until max_splits splits have been made. If max_splits is 0, return the entire string as one piece. Preserve empty pieces caused by leading, trailing, or consecutive separators. Any remaining separator characters after the limit belong to the final piece.

Formal Specification

  • Input: text, a string; separator, a string containing exactly one character; and max_splits, a nonnegative integer.
  • Output: A list of strings in scanning order.
  • Do not use str.split or regular expressions.

Constraints

  • 0 <= len(text) <= 10^5
  • separator contains exactly one character
  • 0 <= max_splits <= 10^5
  • Leading, trailing, and consecutive separators must be preserved

Function Signature

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