🎯 By the end of this lesson you can…
- Apply the six UMPIRE steps
- Ask clarifying questions and list edge cases
- Move from brute force to an optimised solution
UMPIRE
- 1Understandrestate the problem, ask questions, write 2–3 examples including edge cases
- 2Matchwhich pattern does it look like? (hashing, two pointers, BFS…)
- 3Plandescribe the steps in plain words or pseudocode
- 4Implementwrite clean code with good names
- 5Reviewtrace your code with an example, line by line
- 6Evaluatestate time and space complexity; can it be better?
Walkthrough: “Find the first letter that doesn’t repeat”
Understand. Input: a string like "swiss". Output: the first character that appears once ("w"), or "" if none. Questions: uppercase? empty string? → assume lowercase, return "".
Match. Counting how often things appear → hash map.
Plan. Pass 1: count every letter. Pass 2: return the first letter whose count is 1.
1BEGIN2 SET TEXT = "swiss"3 SET COUNTS = {}4 FOR EACH C IN TEXT5 IF CONTAINS(COUNTS, C) THEN6 SET COUNTS[C] = COUNTS[C] + 17 ELSE8 SET COUNTS[C] = 19 END IF10 END FOR11 SET ANSWER = ""12 FOR EACH C IN TEXT13 IF COUNTS[C] = 1 AND ANSWER = "" THEN14 SET ANSWER = C15 END IF16 END FOR17 DISPLAY ANSWER18END
Implement.
First unique character
1function firstUnique(s) {2 const counts = new Map();3 for (const c of s) counts.set(c, (counts.get(c) || 0) + 1);4 for (const c of s) if (counts.get(c) === 1) return c;5 return "";6}7console.log(firstUnique("swiss"));1def first_unique(s):2 counts = {}3 for c in s:4 counts[c] = counts.get(c, 0) + 15 for c in s:6 if counts[c] == 1:7 return c8 return ""9 10print(first_unique("swiss"))✨ The logic didn’t change. Only the syntax changed.
Review. “swiss”: counts s:3, w:1, i:1 → first with count 1 is “w” ✔. Edge case "aa" → "" ✔.
Evaluate. Two passes → O(n) time. The map holds at most 26 letters → O(1) extra space for lowercase letters.
Implement it yourself: return the first character that appears exactly once, or an empty string.
📌 Key takeaways
- Never start coding before you understand the problem and have examples.
- Say the brute force first, then improve it.
- Always test with an edge case before saying "done".
Finished reading & practising?
Saved in this browser. Create a free account to keep it forever.