Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

String A Before B

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

Your question is String A Before B. 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

A phData pipeline validation step receives a sequence containing only A and B labels. Return True if all A labels occur before all B labels. Return False if an A appears after any B.

The sequence may contain any number of consecutive labels, including only one label type. Solve the problem in one left-to-right pass without sorting or modifying the input.

Formal Specification

Implement all_as_before_bs(labels).

  • Input: labels, a string containing only uppercase characters A and B.
  • Output: A boolean. Return True when the string has the form A*B*, where either group may be empty. Otherwise, return False.

Constraints

  • 0 <= len(labels) <= 10^6
  • Every character in labels is either A or B
  • The input must not be modified
  • Target complexity is O(n) time and O(1) auxiliary space

Function Signature

def all_as_before_bs(labels):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output