Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
We define the usage of capitals in a word to be right when one of the following cases holds:
"USA"."leetcode"."Google".Given a string word, return true if the usage of capitals in it is right.
Example 1:
Input: word = "USA" Output: true
Example 2:
Input: word = "FlaG" Output: false
Constraints:
1 <= word.length <= 100word consists of lowercase and uppercase English letters.Problem summary: We define the usage of capitals in a word to be right when one of the following cases holds: All letters in this word are capitals, like "USA". All letters in this word are not capitals, like "leetcode". Only the first letter in this word is capital, like "Google". Given a string word, return true if the usage of capitals in it is right.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"USA"
"FlaG"
capitalize-the-title)count-the-number-of-special-characters-ii)count-the-number-of-special-characters-i)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #520: Detect Capital
class Solution {
public boolean detectCapitalUse(String word) {
int cnt = 0;
for (char c : word.toCharArray()) {
if (Character.isUpperCase(c)) {
++cnt;
}
}
return cnt == 0 || cnt == word.length()
|| (cnt == 1 && Character.isUpperCase(word.charAt(0)));
}
}
// Accepted solution for LeetCode #520: Detect Capital
func detectCapitalUse(word string) bool {
cnt := 0
for _, c := range word {
if unicode.IsUpper(c) {
cnt++
}
}
return cnt == 0 || cnt == len(word) || (cnt == 1 && unicode.IsUpper(rune(word[0])))
}
# Accepted solution for LeetCode #520: Detect Capital
class Solution:
def detectCapitalUse(self, word: str) -> bool:
cnt = sum(c.isupper() for c in word)
return cnt == 0 or cnt == len(word) or (cnt == 1 and word[0].isupper())
// Accepted solution for LeetCode #520: Detect Capital
struct Solution;
impl Solution {
fn detect_capital_use(word: String) -> bool {
let n = word.len();
if n <= 1 {
return true;
}
let word: Vec<char> = word.chars().collect();
let first_is_lowercase: bool = word[0].is_lowercase();
if first_is_lowercase {
for i in 1..n {
if word[i].is_uppercase() {
return false;
}
}
} else {
let mut prev: Option<bool> = None;
for i in 1..n {
if let Some(prev_case) = prev {
if prev_case != word[i].is_uppercase() {
return false;
}
} else {
prev = Some(word[i].is_uppercase());
}
}
}
true
}
}
#[test]
fn test() {
let word = "USA".to_string();
assert_eq!(Solution::detect_capital_use(word), true);
let word = "FlaG".to_string();
assert_eq!(Solution::detect_capital_use(word), false);
}
// Accepted solution for LeetCode #520: Detect Capital
function detectCapitalUse(word: string): boolean {
const cnt = word.split('').reduce((acc, c) => acc + (c === c.toUpperCase() ? 1 : 0), 0);
return cnt === 0 || cnt === word.length || (cnt === 1 && word[0] === word[0].toUpperCase());
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.