Magicsheet logo

Detect Capital

Easy
12.5%
Updated 8/1/2025

Detect Capital

What is this problem about?

The Detect Capital interview question asks you to validate if a word follows standard capitalization rules. A word is valid if:

  1. All letters are capitals (e.g., "USA").
  2. All letters are lowercase (e.g., "google").
  3. Only the first letter is capital (e.g., "Google").

Why is this asked in interviews?

This is a standard "Easy" screening question used by Microsoft and Bloomberg. it's a test of basic string processing and conditional logic. It evaluates whether you can handle multiple valid states cleanly and whether you consider edge cases like single-letter words or empty strings.

Algorithmic pattern used

This problem uses String Manipulation and Case Checking. You can solve it by counting the number of uppercase letters and checking their positions.

  • If count == length     \implies All caps (Valid).
  • If count == 0     \implies All lowercase (Valid).
  • If count == 1 and the first letter is uppercase     \implies Capitalized (Valid).
  • Otherwise     \implies Invalid.

Example explanation

  1. "USA": count=3, length=3. Valid.
  2. "FlaG": count=3 (F, G), but not all are caps and it's not just the first one. Invalid.
  3. "leetcode": count=0. Valid.
  4. "Google": count=1, and 'G' is at index 0. Valid.

Common mistakes candidates make

  • Redundant Checks: Writing a massive chain of if-else statements that check character by character without a summary count.
  • Index errors: Not correctly identifying if only the first letter is capital.
  • Inefficiency: Converting the whole string to uppercase or lowercase multiple times, though for short words this is usually fine.

Interview preparation tip

For string validation problems, try to find a mathematical way to summarize the state (like counting capitals) before jumping into complex conditional logic. It makes the code much more robust and readable.

Similar Questions