Alternating Signal Sequences — Problem Statement & Solution Guide

StringsMediumMixed
TimeO(n)
|
SpaceO(1)

Quick Answer & Algorithm Key Takeaway

Master coding challenges related to Strings and solve the Alternating Signal Sequences problem optimally.

TopicStrings
PatternMixed
TimeO(n)
SpaceO(1)

Problem Description

Given a string S consisting solely of the characters 'A', 'B' and 'C', determine the maximum length of a contiguous substring that qualifies as a *valid alternating block*. A substring is a valid alternating block if it satisfies all of the following conditions: (1) Its characters follow a repeating cycle of three distinct symbols. The cycle can be any permutation of the three letters, for example "ABCABC..." or "ACBACB...". (2) The first character of the block is either 'A' or 'C'. (3) The block ends with the same character it starts with, which implies that its length is of the form 3·k + 1 for some integer k ≥ 0 (k = 0 corresponds to a single‑character block). Return the length of the longest such block present in S. If no block longer than one character exists, the answer is 1 because any single 'A' or 'C' trivially satisfies the definition.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Alternating Signal Sequences"

medium

WHY DOES IT MATTER?

Detecting fixed‑length periodic patterns is a core skill for string processing, compression, and protocol validation; mastering it shows you can turn combinatorial constraints into linear scans.

OPTIMIZATION CHALLENGE

The key insight is that the cycle order is constant and limited, so you can pre‑enumerate all possible orders and validate each in a single pass, collapsing what appears to be a combinatorial explosion into O(1) extra work per character.

REAL-WORLD CONNECTION

Think of a rotating LED indicator that cycles through three colors. Monitoring the longest uninterrupted correct cycle in a sensor stream mirrors this problem, just as distributed systems must verify heartbeat sequences across nodes.

When coding, first generate the six permutations, then loop over them with a shared index variable; reuse the same counter variable for current length to avoid extra arrays, and update the global maximum on the fly.

COMPLEXITY AT A GLANCE

⏱ Time:O(n)
💾 Space:O(1)

Core Theory — Why This Approach?

The problem reduces to detecting the longest contiguous segment that conforms to a fixed 3‑character cycle. A naive scan that checks every possible substring would be O(n^2) and quickly exceeds limits for n up to 10^6. The optimal paradigm treats the cycle as a periodic template: for each of the six permutations of the set {A,B,C} we compute the longest run where S[i] equals the expected character at position i%3. This can be done in a single linear pass per permutation, maintaining a running length and resetting when a mismatch occurs. Because the number of permutations is constant (3! = 6), the overall time stays O(n) while using O(1) extra space.

Interview Questions on This Problem

Q1How would you modify the solution if the alphabet size were arbitrary (e.g., any k distinct characters) and the cycle length equals k?

Generalize the approach: generate the k! possible permutations of the k characters (or, more efficiently, treat the cycle as any ordering and use a sliding window that tracks the last occurrence of each character modulo k). The linear scan now checks S[i] against expectedChar = perm[i%k]; time remains O(k!·n) which is feasible only for small k, so for larger k you would use a hashmap to store the expected character for each residue class and update on the fly, achieving O(n) time and O(k) space.

Q2Why does a two‑pointer sliding window not improve over the per‑permutation scan for this specific problem?

A sliding window requires a dynamic way to verify that the window respects a consistent 3‑cycle, which essentially means the window’s start determines the whole ordering. Changing the start shifts the expected characters for all positions, making constant‑time validation impossible without recomputing the pattern. Hence the simplest O(n) solution is to fix the pattern upfront (six possibilities) and scan, rather than maintain a mutable window.

Q3In a distributed log‑processing system, how could you compute the longest alternating block across partition boundaries?

Each partition can compute its local longest block, the prefix length that matches a given cycle, and the suffix length that matches the same cycle. A coordinator then merges these summaries for each of the six cycles, concatenating suffix of one partition with prefix of the next to possibly form a longer block, achieving a linear‑time reduction across nodes.

Examples

Example 1

Input

ABCA

Output

4

Explanation: The whole string "ABCA" follows the cycle A→B→C→A. It starts with 'A', ends with 'A', and its length 4 equals 3·1+1, so it is a valid alternating block. No longer block exists, therefore the answer is 4.

Example 2

Input

ACBACBAC

Output

7

Explanation: The prefix "ACBACBA" (positions 1‑7) repeats the cycle A→C→B and ends with the starting character 'A'. Its length is 7 = 3·2+1, satisfying all conditions. Extending to the full string would end with 'C', breaking condition 3, so the maximum length is 7.

Example 3

Input

CCABCA

Output

4

Explanation: The substring from index 2 to 5 is "CABC". It follows the cycle C→A→B and returns to 'C' at the end, giving a length of 4 = 3·1+1. No longer substring meets the criteria, so the answer is 4.

Constraints

  • 1 <= |S| <= 200000
  • S contains only the characters 'A', 'B', and 'C'
  • The algorithm should run in O(|S|) time and O(1) additional memory

Optimal Approach & Strategy

Iterate over the six fixed permutations of ABC, scanning once per permutation and maintaining a running match length, yielding O(n) time.

Brute Force Approach

Check every possible substring, verify if it forms a three‑character cycle, and keep the longest—this is O(n^2).

Code Solutions

JavaScript Solution
Time: O(n)
/**
 * @param {string} s - The input string consisting of 'A', 'B', and 'C'.
 * @return {number} - The maximum length of a valid alternating block.
 */
function maxAlternatingBlockLength(s) {
    const n = s.length;
    if (n === 0) return 0;
    
    const patterns = ["ABC", "ACB", "BAC", "BCA", "CAB", "CBA"];
    let maxLen = 0;
    
    for (let i = 0; i < n; i++) {
        for (const pat of patterns) {
            if (s[i] !== pat[0]) continue;
            
            let len = 1;
            while (i + len < n && s[i + len] === pat[len % 3]) {
                len++;
            }
            maxLen = Math.max(maxLen, len);
        }
    }
    
    return maxLen;
}

// Driver code
const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    terminal: false
});

rl.on('line', (line) => {
    const s = line.trim();
    console.log(maxAlternatingBlockLength(s));
    rl.close();
});

Asked in Top Tech Interviews

Atlassian

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.