Problem · Array

Incremental String Qualification with Split and Merge

Learn this problem
EasyStripe logoStripeFULLTIMEPHONE SCREEN
See Stripe hiring insights

Problem statement

An ordered list of nonempty strings, initialParts, represents one logical string partitioned into adjacent parts. You will process a finite sequence of split and merge operations while preserving the order of all characters.

Operations

  • ["SPLIT", i, offset]: replace part i with its nonempty prefix of length offset followed by its remaining nonempty suffix.
  • ["MERGE", i]: replace parts i and i + 1 with their concatenation.

After each operation, classify the current partition:

  • QUALIFIED if every current part has length at most maxPartLength.
  • UNQUALIFIED otherwise.

Return one classification per operation, in the same order as operations.

Function

classifyParts(initialParts: String[], operations: String[][], maxPartLength: int) → String[]

Examples

Example 1

initialParts = ["abcdef"]operations = [["SPLIT","0","3"],["MERGE","0"],["SPLIT","0","2"],["SPLIT","1","2"]]maxPartLength = 3return = ["QUALIFIED","UNQUALIFIED","UNQUALIFIED","QUALIFIED"]

Splitting abcdef at offset 3 produces [abc, def], so both parts qualify. Merging restores one length-6 part. The next split produces lengths 2 and 4, and the final split produces three length-2 parts.

Example 2

initialParts = ["ab","cdef"]operations = [["MERGE","0"],["SPLIT","0","4"]]maxPartLength = 4return = ["UNQUALIFIED","QUALIFIED"]

The merge creates abcdef, whose length exceeds 4. Splitting it into abcd and ef makes every part short enough.

Example 3

initialParts = ["a","bc","def"]operations = [["MERGE","0"],["MERGE","0"],["SPLIT","0","3"]]maxPartLength = 3return = ["QUALIFIED","UNQUALIFIED","QUALIFIED"]

The first merge creates abc, which still qualifies. The second creates one length-6 part, and splitting it at offset 3 restores two qualified parts.

Constraints

  • 1 ≤ initialParts.length ≤ 2000.
  • 1 ≤ operations.length ≤ 2000.
  • 1 ≤ maxPartLength ≤ 100.
  • Each initial part contains only lowercase English letters and has length in [1, 100].
  • The total number of characters across all parts is at most 2000 and remains constant.
  • Every operation is either SPLIT or MERGE with the exact arity shown above.
  • Every split index and offset are valid for the current partition and produce two nonempty parts.
  • Every merge index has a right-adjacent part in the current partition.

More Stripe problems

drafts saved locally
public String[] classifyParts(String[] initialParts, String[][] operations, int maxPartLength) {
    // Write your code here.
}
initialParts["abcdef"]
operations[["SPLIT","0","3"],["MERGE","0"],["SPLIT","0","2"],["SPLIT","1","2"]]
maxPartLength3
expected["QUALIFIED", "UNQUALIFIED", "UNQUALIFIED", "QUALIFIED"]
checking account