Magicsheet logo

Camelcase Matching

Medium
89.4%
Updated 6/1/2025

Camelcase Matching

What is this problem about?

The Camelcase Matching interview question involves matching a list of query strings against a specific pattern. A query matches the pattern if we can insert lowercase letters into the pattern to make it equal to the query. Critically, we cannot insert any uppercase letters. This Camelcase Matching coding problem is a test of string filtering based on specific structural rules.

Why is this asked in interviews?

Companies like Google and Compass use this to test a candidate's proficiency with string traversal and character-by-character comparison. It evaluates whether you can handle constraints (like the "no extra uppercase" rule) while effectively matching a subsequence. It's a great test for two-pointer logic or regular expression intuition.

Algorithmic pattern used

The most efficient approach follows the Two Pointers, String interview pattern.

  1. Use one pointer for the query and one for the pattern.
  2. As you move through the query, if the characters match, move the pattern pointer.
  3. If the characters don't match, the character in the query must be lowercase. If it's uppercase and doesn't match the current pattern character, the match fails.
  4. After traversing the query, check if you successfully matched all characters in the pattern.

Example explanation

pattern = "FB", query = "FooBar"

  1. 'F' matches 'F'. Pattern pointer moves to 'B'.
  2. 'o', 'o' in query are lowercase. Skip them.
  3. 'B' matches 'B'. Pattern pointer moves past the end.
  4. 'a', 'r' in query are lowercase. Skip them. Match: True. If query = "FoBaR", it fails because 'R' is an extra uppercase letter not found in the pattern.

Common mistakes candidates make

  • Missing extra uppercase letters: Forgetting to check characters in the query that don't appear in the pattern. If they are uppercase, it's an automatic mismatch.
  • Incorrect subsequence matching: Failing to ensure that the pattern characters appear in the query in the correct order.
  • Over-complicating with Regex: While possible, regex can be tricky to build dynamically for this specific "no extra uppercase" constraint.

Interview preparation tip

When a problem asks if one string can be "built" from another by adding specific types of characters, always start with a two-pointer subsequence check. It's usually the most optimal O(N) solution.

Similar Questions