From debf51f7d9222f345704faa79f8090ad4ae5e8cc Mon Sep 17 00:00:00 2001 From: rsimpson2 Date: Thu, 12 Feb 2026 08:29:32 -0500 Subject: [PATCH 1/6] learn: cycle 1 - handle empty array edge case --- .../README.md | 27 +++++++++++++++++++ .../reverse-words-in-a-string-ii.test.ts | 10 +++++++ .../reverse-words-in-a-string-ii.ts | 5 ++++ 3 files changed, 42 insertions(+) create mode 100644 leetcode/medium/0186-reverse-words-in-a-string-ii/README.md create mode 100644 leetcode/medium/0186-reverse-words-in-a-string-ii/reverse-words-in-a-string-ii.test.ts create mode 100644 leetcode/medium/0186-reverse-words-in-a-string-ii/reverse-words-in-a-string-ii.ts diff --git a/leetcode/medium/0186-reverse-words-in-a-string-ii/README.md b/leetcode/medium/0186-reverse-words-in-a-string-ii/README.md new file mode 100644 index 0000000..af59b10 --- /dev/null +++ b/leetcode/medium/0186-reverse-words-in-a-string-ii/README.md @@ -0,0 +1,27 @@ +# 186. Reverse Words in a String II + +**Difficulty:** ![Medium](https://img.shields.io/badge/Medium-orange) + +**Link:** [LeetCode Problem 186](https://leetcode.com/problems/reverse-words-in-a-string-ii/) + +## Problem Description + +Given an input string as a character array, reverse the string word by word. + +## Examples + +### Example 1 + +**Input:** `["t","h","e"," ","s","k","y"," ","i","s"," ","b","l","u","e"]` + +**Output:** `["b","l","u","e"," ","i","s"," ","s","k","y"," ","t","h","e"]` + +## Constraints + +- A word consists of non-space characters only +- The input has no leading or trailing spaces +- Words are separated by single spaces + +## Follow-up + +Could you do it in-place without allocating extra space? diff --git a/leetcode/medium/0186-reverse-words-in-a-string-ii/reverse-words-in-a-string-ii.test.ts b/leetcode/medium/0186-reverse-words-in-a-string-ii/reverse-words-in-a-string-ii.test.ts new file mode 100644 index 0000000..bb58c54 --- /dev/null +++ b/leetcode/medium/0186-reverse-words-in-a-string-ii/reverse-words-in-a-string-ii.test.ts @@ -0,0 +1,10 @@ +import { describe, it, expect } from 'vitest'; +import { reverseWords } from './reverse-words-in-a-string-ii'; + +describe('Reverse Words in a String II', () => { + it('should handle empty string (no words to reverse)', () => { + const input: string[] = []; + reverseWords(input); + expect(input).toEqual([]); + }); +}); diff --git a/leetcode/medium/0186-reverse-words-in-a-string-ii/reverse-words-in-a-string-ii.ts b/leetcode/medium/0186-reverse-words-in-a-string-ii/reverse-words-in-a-string-ii.ts new file mode 100644 index 0000000..53b7d67 --- /dev/null +++ b/leetcode/medium/0186-reverse-words-in-a-string-ii/reverse-words-in-a-string-ii.ts @@ -0,0 +1,5 @@ +export function reverseWords(s: string[]): void { + const length = s.length; + + if (length === 0) return; +} From 289f35a3ab9b2e1e7e4224d65171ae4a7785dfad Mon Sep 17 00:00:00 2001 From: rsimpson2 Date: Thu, 12 Feb 2026 09:15:55 -0500 Subject: [PATCH 2/6] learn: cycle 2 - implement two-step word reversal algorithm --- .../reverse-words-in-a-string-ii.test.ts | 6 ++++ .../reverse-words-in-a-string-ii.ts | 33 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/leetcode/medium/0186-reverse-words-in-a-string-ii/reverse-words-in-a-string-ii.test.ts b/leetcode/medium/0186-reverse-words-in-a-string-ii/reverse-words-in-a-string-ii.test.ts index bb58c54..24c91a2 100644 --- a/leetcode/medium/0186-reverse-words-in-a-string-ii/reverse-words-in-a-string-ii.test.ts +++ b/leetcode/medium/0186-reverse-words-in-a-string-ii/reverse-words-in-a-string-ii.test.ts @@ -7,4 +7,10 @@ describe('Reverse Words in a String II', () => { reverseWords(input); expect(input).toEqual([]); }); + + it('should reverse two single-character words', () => { + const input = ['b', ' ', 'a']; + reverseWords(input); + expect(input).toEqual(['a', ' ', 'b']); + }); }); diff --git a/leetcode/medium/0186-reverse-words-in-a-string-ii/reverse-words-in-a-string-ii.ts b/leetcode/medium/0186-reverse-words-in-a-string-ii/reverse-words-in-a-string-ii.ts index 53b7d67..afd7f99 100644 --- a/leetcode/medium/0186-reverse-words-in-a-string-ii/reverse-words-in-a-string-ii.ts +++ b/leetcode/medium/0186-reverse-words-in-a-string-ii/reverse-words-in-a-string-ii.ts @@ -2,4 +2,37 @@ export function reverseWords(s: string[]): void { const length = s.length; if (length === 0) return; + + // Step 1: Reverse the entire array + let left = 0; + let right = length - 1; + + while (left < right) { + [s[left], s[right]] = [s[right], s[left]]; + left++; + right--; + } + + // Step 2: Reverse each individual word + let i = 0; + + while (i < length) { + while (i < length && s[i] === ' ') { + i++; + } + + let start = i; + + while (i < length && s[i] !== ' ') { + i++; + } + + let end = i - 1; + + while (start < end) { + [s[start], s[end]] = [s[end], s[start]]; + start++; + end--; + } + } } From 7b265b9d2d04ba1f3c82ea24cbacc3796e7bfd40 Mon Sep 17 00:00:00 2001 From: rsimpson2 Date: Thu, 12 Feb 2026 09:23:16 -0500 Subject: [PATCH 3/6] learn: cycle 3 - add test for multiple multi-character words (already passing) --- .../reverse-words-in-a-string-ii.test.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/leetcode/medium/0186-reverse-words-in-a-string-ii/reverse-words-in-a-string-ii.test.ts b/leetcode/medium/0186-reverse-words-in-a-string-ii/reverse-words-in-a-string-ii.test.ts index 24c91a2..b370403 100644 --- a/leetcode/medium/0186-reverse-words-in-a-string-ii/reverse-words-in-a-string-ii.test.ts +++ b/leetcode/medium/0186-reverse-words-in-a-string-ii/reverse-words-in-a-string-ii.test.ts @@ -13,4 +13,11 @@ describe('Reverse Words in a String II', () => { reverseWords(input); expect(input).toEqual(['a', ' ', 'b']); }); + + it('should reverse multiple multi-character words', () => { + const input = ['c', 'a', 't', ' ', 'd', 'o', 'g']; + const expected = ['d', 'o', 'g', ' ', 'c', 'a', 't']; + reverseWords(input); + expect(input).toEqual(expected); + }); }); From ca041a6ac2c21f2bcbb59680ee281c9e2f650380 Mon Sep 17 00:00:00 2001 From: rsimpson2 Date: Thu, 12 Feb 2026 09:27:25 -0500 Subject: [PATCH 4/6] learn: cycle 4 - add test for single word edge case (already passing) --- .../reverse-words-in-a-string-ii.test.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/leetcode/medium/0186-reverse-words-in-a-string-ii/reverse-words-in-a-string-ii.test.ts b/leetcode/medium/0186-reverse-words-in-a-string-ii/reverse-words-in-a-string-ii.test.ts index b370403..d895894 100644 --- a/leetcode/medium/0186-reverse-words-in-a-string-ii/reverse-words-in-a-string-ii.test.ts +++ b/leetcode/medium/0186-reverse-words-in-a-string-ii/reverse-words-in-a-string-ii.test.ts @@ -20,4 +20,11 @@ describe('Reverse Words in a String II', () => { reverseWords(input); expect(input).toEqual(expected); }); + + it('should handle single word (no spaces)', () => { + const input = ['s', 'i', 'n', 'g', 'l', 'e']; + const expected = ['s', 'i', 'n', 'g', 'l', 'e']; + reverseWords(input); + expect(input).toEqual(expected); + }); }); From fdaf941f0c3a19eed43b0c81bfd7416234448593 Mon Sep 17 00:00:00 2001 From: rsimpson2 Date: Thu, 12 Feb 2026 09:30:22 -0500 Subject: [PATCH 5/6] learn: cycle 5 - learner-authored test for main example (already passing) --- .../reverse-words-in-a-string-ii.test.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/leetcode/medium/0186-reverse-words-in-a-string-ii/reverse-words-in-a-string-ii.test.ts b/leetcode/medium/0186-reverse-words-in-a-string-ii/reverse-words-in-a-string-ii.test.ts index d895894..3b9355e 100644 --- a/leetcode/medium/0186-reverse-words-in-a-string-ii/reverse-words-in-a-string-ii.test.ts +++ b/leetcode/medium/0186-reverse-words-in-a-string-ii/reverse-words-in-a-string-ii.test.ts @@ -27,4 +27,11 @@ describe('Reverse Words in a String II', () => { reverseWords(input); expect(input).toEqual(expected); }); + + it('should handle more than 2 words', () => { + const input = ['t', 'h', 'e', ' ', 's', 'k', 'y', ' ', 'i', 's', ' ', 'b', 'l', 'u', 'e']; + const expected = ['b', 'l', 'u', 'e', ' ', 'i', 's', ' ', 's', 'k', 'y', ' ', 't', 'h', 'e']; + reverseWords(input); + expect(input).toEqual(expected); + }); }); From 980c33d2fb4a0ac8ba96ecfc115e8670dd2f5906 Mon Sep 17 00:00:00 2001 From: rsimpson2 Date: Thu, 12 Feb 2026 09:34:16 -0500 Subject: [PATCH 6/6] learn: solve Reverse Words in a String II (#186) via guided TDD --- docs/PROBLEMS.md | 2 + .../POST_MORTEM.md | 207 ++++++++++++++++++ 2 files changed, 209 insertions(+) create mode 100644 leetcode/medium/0186-reverse-words-in-a-string-ii/POST_MORTEM.md diff --git a/docs/PROBLEMS.md b/docs/PROBLEMS.md index 1a66a26..2e128b5 100644 --- a/docs/PROBLEMS.md +++ b/docs/PROBLEMS.md @@ -130,6 +130,7 @@ Complete list of all solved problems, organized by difficulty and topic. - [0073 - Set Matrix Zeroes](../leetcode/medium/0073-set-matrix-zeroes) ![Medium](https://img.shields.io/badge/Medium-orange) - [0080 - Remove Duplicates from Sorted Array II](../leetcode/medium/0080-remove-duplicates-from-sorted-array-ii) ![Medium](https://img.shields.io/badge/Medium-orange) - [0090 - Subsets II](../leetcode/medium/0090-subsets-ii) ![Medium](https://img.shields.io/badge/Medium-orange) +- [0186 - Reverse Words in a String II](../leetcode/medium/0186-reverse-words-in-a-string-ii) ![Medium](https://img.shields.io/badge/Medium-orange) - [0209 - Minimum Size Subarray Sum](../leetcode/medium/0209-minimum-size-subarray-sum) ![Medium](https://img.shields.io/badge/Medium-orange) - [0287 - Find the Duplicate Number](../leetcode/medium/0287-find-the-duplicate-number) ![Medium](https://img.shields.io/badge/Medium-orange) - [0324 - Wiggle Sort II](../leetcode/medium/0324-wiggle-sort-ii) ![Medium](https://img.shields.io/badge/Medium-orange) @@ -267,6 +268,7 @@ Complete list of all solved problems, organized by difficulty and topic. - [0016 - 3Sum Closest](../leetcode/medium/0016-3sum-closest) - Medium - [0018 - 4Sum](../leetcode/medium/0018-4sum) - Medium - [0080 - Remove Duplicates from Sorted Array II](../leetcode/medium/0080-remove-duplicates-from-sorted-array-ii) - Medium +- [0186 - Reverse Words in a String II](../leetcode/medium/0186-reverse-words-in-a-string-ii) - Medium - [More in technique guide](./techniques/TWO_POINTERS.md) ### Sliding Window diff --git a/leetcode/medium/0186-reverse-words-in-a-string-ii/POST_MORTEM.md b/leetcode/medium/0186-reverse-words-in-a-string-ii/POST_MORTEM.md new file mode 100644 index 0000000..5bfb472 --- /dev/null +++ b/leetcode/medium/0186-reverse-words-in-a-string-ii/POST_MORTEM.md @@ -0,0 +1,207 @@ +# Interview Debrief — Reverse Words in a String II + +**Date:** 2026-02-12 +**Problem:** [Reverse Words in a String II](https://leetcode.com/problems/reverse-words-in-a-string-ii/) — Medium +**Duration:** ~90 minutes (estimated from 5 cycles) +**Mode:** Guided TDD (Coach Mode) + +**Duration Estimation:** + +- 5 cycles × ~18 minutes per cycle = ~90 minutes +- Cycle 2 took significantly longer due to debugging iterations +- Includes pattern checkpoint discussions and hint escalation + +--- + +## Session Overview + +The candidate demonstrated solid problem-solving fundamentals and good perseverance through debugging challenges. They successfully implemented a two-step array reversal algorithm using the two-pointer pattern, though they required moderate coaching support (Level 2 hints) to arrive at the correct structure. Communication was clear throughout, and they showed willingness to work through multiple iterations to get the solution right. The candidate progressed well through the test-ownership ladder, ultimately authoring a complete test independently at Level 3. + +--- + +## Problem Solving + +### Score: 3/5 + +**Strengths:** + +- Correctly identified the Two-Pointer pattern early on +- Good edge case instincts (started with empty array test) +- Successfully articulated the two-step approach (reverse entire array, then reverse each word) after initial confusion +- Showed logical progression from simple to complex test cases + +**Areas for Improvement:** + +- Initially misunderstood the problem statement (confused character reversal with word-order reversal in cycle 2) +- Took several clarifying questions to grasp that "reverse words" means reversing word order, not characters +- Required Level 2 hints (algorithm structure) to implement the word-reversal logic correctly +- Did not independently arrive at the three-nested-loops structure for Step 2 + +**Help Needed:** Moderate — Needed Level 2 pattern hints (algorithm structure with pseudocode) in cycle 2 after multiple failed attempts. Once the structure was provided, successfully implemented it. + +--- + +## Coding + +### Score: 3.5/5 + +**Strengths:** + +- Clean, readable code with good variable naming +- Proper use of TypeScript destructuring for swaps: `[s[left], s[right]] = [s[right], s[left]]` +- Correctly implemented Step 1 (reverse entire array) independently +- Good code structure with clear comments separating the two steps +- Successfully identified and fixed the `start = i` bug after guidance + +**Areas for Improvement:** + +- Initial implementations of Step 2 had fundamental logic errors (swapping word characters with space characters) +- Struggled to translate the three-loop structure into code without seeing explicit pseudocode +- Made a variable initialization error (`start = 0` instead of `start = i`) that took guidance to spot +- Needed multiple debugging iterations before getting Step 2 correct + +**Debugging Process:** + +- Responded well to test failures, didn't give up +- Required guiding questions to trace through logic manually +- Once pointed toward the specific bug (variable initialization), fixed it immediately +- Showed good debugging patience but needed coaching to identify root causes + +--- + +## Verification + +### Score: 4/5 + +**Strengths:** + +- Proposed strong edge cases: empty array (cycle 1), single word (cycle 4) +- Understood the importance of testing progressively complex scenarios +- At Level 2, correctly specified expected outputs for test cases +- At Level 3, independently authored a complete test for the main example +- Caught and corrected an incorrect assertion pattern (`expect(input).toEqual(input)`) when prompted + +**Areas for Improvement:** + +- Did not proactively trace through examples before coding (would have caught the logic error earlier) +- Initially proposed an invalid assertion comparing a variable to itself +- Could have been more proactive in verifying invariants during implementation + +--- + +## Communication + +### Score: 4/5 + +**Strengths:** + +- Clearly articulated pattern and invariants when prompted +- Asked clarifying questions when confused about expected outputs +- Good use of technical vocabulary (two-pointer, invariant, edge case) +- Responded well to follow-up questions, refining answers appropriately +- Explicit about when ready to proceed ("continue") + +**Areas for Improvement:** + +- Pattern checkpoint in cycle 1: Initially said `.length` is O(n) (thought it requires iteration), corrected after gentle hint +- Could have communicated their thinking process more during implementation (worked somewhat silently) +- When stuck, could have verbalized confusion earlier to get help sooner + +--- + +## Complexity Analysis + +### Score: 4/5 + +**Strengths:** + +- Correctly identified O(n) time complexity for the full solution +- Correctly identified O(1) space complexity (constant variables, in-place modification) +- Explained reasoning: "Each character got touched once in each of the loops" +- Understood that multiple linear passes still result in O(n) overall + +**Areas for Improvement:** + +- Initial misunderstanding about array `.length` property (thought it was O(n) instead of O(1)) +- Reasoning could have been more precise (each character is touched multiple times in different sub-operations, not just once per loop) +- Did not explicitly mention amortized analysis or break down each step's complexity + +--- + +## Pattern Recognition + +- **Pattern Used:** Two-Pointer (applied twice — once for entire array, once per word) +- **Key Insight:** Reverse the entire array first to get words in the right order (but characters backwards), then reverse each individual word to fix character order. The "aha moment" came after Level 2 hints in cycle 2. +- **Pattern Checkpoint Results:** 2/2 correct on first attempt (with minor correction about `.length` complexity) +- **Related Problems to Practice:** + - [151. Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) — Same problem but with string manipulation instead of character array (practices boundary detection) + - [557. Reverse Words in a String III](https://leetcode.com/problems/reverse-words-in-a-string-iii/) — Reverse characters in each word but keep word order (practices word-boundary detection) + - [189. Rotate Array](https://leetcode.com/problems/rotate-array/) — Uses similar three-reversal technique (reinforces two-pointer reversal pattern) + +--- + +## Session Progression + +The candidate started strong with good edge case instincts (empty array) and correct pattern identification. Cycle 2 presented the main challenge: they initially misunderstood the problem (character reversal vs. word-order reversal), then struggled significantly with implementing the word-reversal logic despite understanding the two-step approach conceptually. After escalating to Level 2 hints (providing algorithm structure), they implemented it successfully but had a variable initialization bug that required guidance to spot. Cycles 3-5 went smoothly as the algorithm was already general enough to handle additional test cases. The candidate showed good growth in test ownership, advancing from proposing test ideas (L1) to authoring complete tests independently (L3). + +| Cycle | What Was Tested | Test Ownership Level | Hint Level | Pattern Checkpoint | Notes | +| ----- | ----------------------- | --------------------- | ---------- | ------------------ | ------------------------------------------------------------------------------------------------------ | +| 1 | Empty array edge case | L1 (Test Intent) | 0 | PASS | Good edge case instinct; minor .length complexity correction | +| 2 | Two single-char words | L1 (Test Intent) | 2 | PASS | Debugging cycle; misunderstood problem initially; needed L2 algorithm structure; fixed `start = i` bug | +| 3 | Two multi-char words | L2 (Assertion Sketch) | 0 | N/A | Test passed immediately (algorithm already general) | +| 4 | Single word (no spaces) | L2 (Assertion Sketch) | 0 | N/A | Corrected invalid assertion; test passed immediately | +| 5 | Main example (4 words) | L3 (Full Test) | 0 | N/A | Independently authored complete test; passed immediately | + +--- + +## No-Hire Trigger Check + +- **Critical Triggers Observed:** None +- **Recovery Evidence:** N/A (no critical triggers) +- **Guardrail Applied:** None + +**Trigger evaluation:** + +- Could not explain core invariant after coaching? → **No** — Successfully explained `left < right` invariant after follow-up questions +- Needed Level 3 hints for core algorithm more than once? → **No** — Only needed Level 2 hints once (cycle 2) +- Could not explain final time/space complexity accurately? → **No** — Correctly identified O(n)/O(1) +- Could not justify correctness? → **No** — Understood the two-step algorithm and why it works +- Repeated same failing assertion 3+ times without new strategy? → **No** — Adjusted approach after each failure +- Left critical bug unresolved? → **No** — Fixed all bugs by session end + +--- + +## Overall Assessment + +**Interview Recommendation:** Lean Recommend +**Hiring Decision:** Lean Hire +**Would Move Forward to Next Round:** Yes +**Confidence:** Medium + +**Summary:** +This candidate demonstrates solid fundamentals and good problem-solving instincts, though they needed moderate coaching support to arrive at the correct solution. They correctly identified the two-pointer pattern and the two-step approach, but struggled to translate that understanding into working code without algorithmic structure hints. The need for Level 2 hints indicates they're not yet at a fully independent level for medium-difficulty problems, but their strong debugging persistence, clear communication, and ability to learn quickly from hints are positive indicators. I would move forward with a lean recommendation, as they show promise but would benefit from more practice with nested-loop scenarios and translating algorithmic intuition into implementation. + +**Strengths to Build On:** + +- Strong edge case instincts (started with empty array, thought of single word) +- Good communication and technical vocabulary usage +- Perseverance through debugging challenges, willingness to iterate +- Successfully progressed through test-ownership ladder to L3 +- Correct complexity analysis (after minor corrections) + +**Priority Areas for Growth:** + +- **Pattern Implementation:** Practice translating algorithmic patterns into code independently without pseudocode scaffolding (focus on nested-loop structures) +- **Problem Comprehension:** Spend more time understanding problem statements and working through examples manually before coding +- **Debugging Strategy:** Develop systematic approaches to trace through logic manually when tests fail (e.g., line-by-line execution with sample inputs) +- **Invariant Verification:** Be more explicit about checking invariants during implementation, not just during pattern checkpoints + +**Recommended Next Steps:** + +- Practice more two-pointer problems with nested loops: [344. Reverse String](https://leetcode.com/problems/reverse-string/), [151. Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/), [189. Rotate Array](https://leetcode.com/problems/rotate-array/) +- Focus on word/substring boundary detection problems to strengthen that skill +- Work on translating high-level algorithm descriptions into code structure independently +- Practice explaining complexity analysis more precisely (e.g., "each character touched at most k times" rather than "once per loop") +- Before coding, trace through at least one example manually on paper to verify understanding + +---