Magicsheet logo

Consecutive Characters

Easy
12.5%
Updated 8/1/2025

Topics

Consecutive Characters

What is this problem about?

The Consecutive Characters interview question asks you to find the "power" of a string. The power is defined as the maximum length of a non-empty substring that contains only one unique character. Essentially, you are looking for the longest streak of identical adjacent characters in a given string.

Why is this asked in interviews?

Companies like Goldman Sachs and Meta use the Consecutive Characters coding problem to test a candidate's basic string manipulation and loop control skills. It is an "Easy" difficulty problem that helps interviewers see if you can implement a single-pass solution with clean logic and no unnecessary overhead.

Algorithmic pattern used

This utilizes the String interview pattern, specifically a linear scan or a "Two Pointers" (sliding window) approach. You iterate through the string once, keeping track of the current streak length and the global maximum length found so far.

Example explanation

Take the string s = "abbcccddddeee".

  1. Start at 'a'. Current streak = 1. Max = 1.
  2. Move to 'b'. New character. Reset streak to 1.
  3. Next is 'b'. Same character. Streak becomes 2. Max = 2.
  4. Move to 'c'. New character. Reset streak to 1.
  5. Next is 'c', then 'c'. Streak becomes 3. Max = 3. In this case, 'd' repeats 4 times, so the final "power" is 4.

Common mistakes candidates make

  • Off-by-one errors: Not counting the last character of the string or resetting the counter too late.
  • Inefficient slicing: Creating many substrings just to check their length, which turns an O(N) problem into O(N^2).
  • Initialization: Starting the maximum length at 0 instead of 1 (for non-empty strings).

Interview preparation tip

Practice "One-Pass" logic. Whenever you see a problem that requires finding a maximum or minimum property in a linear sequence, your first instinct should be a single for loop with a few state variables.

Similar Questions