Magicsheet logo

Number of Changing Keys

Easy
97.9%
Updated 6/1/2025

Asked by 3 Companies

Topics

Number of Changing Keys

What is this problem about?

The Number of Changing Keys problem gives you a string representing keystrokes. Each character represents a key pressed. Count how many times the key changes from one character to the next — case-insensitively. This Number of Changing Keys coding problem is a simple string comparison exercise.

Why is this asked in interviews?

Apple, Autodesk, and Amazon ask this easy problem to quickly test string traversal and case-insensitive comparison. It validates that candidates handle character comparison correctly, apply lowercase/uppercase normalization, and write clean loops. The string interview pattern is demonstrated at its most fundamental.

Algorithmic pattern used

Single pass comparison. Convert the string to lowercase (or uppercase). For each consecutive pair of characters, if they differ, increment the count. Return the total count.

Example explanation

Input: "aAbBcC". Lowercase: "aabbcc".

  • 'a'→'a': same.
  • 'a'→'b': different. Count=1.
  • 'b'→'b': same.
  • 'b'→'c': different. Count=2.
  • 'c'→'c': same. Result = 2.

Input: "AaA": lowercase "aaa" → 0 changes. Result = 0.

Common mistakes candidates make

  • Comparing case-sensitively ('A' ≠ 'a' would give wrong answer).
  • Starting the loop at index 0 instead of index 1.
  • Off-by-one: iterating n times instead of n-1 times.
  • Counting a single-character string as having a change.

Interview preparation tip

This is a warm-up problem. The key takeaway is: always normalize case before comparison. s.lower() in Python handles this in one call. Practice writing clean string comparison loops — checking s[i] != s[i-1] is the canonical pattern for "count transitions." This same pattern applies to run-length encoding, counting distinct runs, and "number of direction changes" problems.

Similar Questions