Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
Does the input have leading or trailing spaces?
Does input have more than a single space between a word?
Can the input be empty?
HAPPY CASE
Input: s = ["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"]
Input: ["h","e","l","l","o"," ","w","o","r","l","d"]
Output: ["w","o","r","l","d"," ","h","e","l","l","o"]
EDGE CASE (Multiple Spaces)
Input: s = ["a"]
Output: ["a"]
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For Array/Strings, common solution patterns include:
Sort
Two pointer solutions (left and right pointer variables)
Storing the elements of the array in a HashMap or a Set
Traversing the array with a sliding window
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Reverse the Whole List and Then Reverse Each Word in the List
1. Reverse the whole input List in place
2. Go through the input List
3. Determine where the word end - find each word in the input List
4. Reverse Each Word in the input List in place
⚠️ Common Mistakes
Implement the code to solve the algorithm.
class Solution:
def reverseWords(self, s: List[str]) -> None:
# 1. Reverse the whole input List in place
self.reverse(s, 0, len(s) - 1)
# 4. Reverse Each Word in the input List in place
self.reverse_each_word(s)
def reverse(self, l: list, left: int, right: int) -> None:
while left < right:
l[left], l[right] = l[right], l[left]
left, right = left + 1, right - 1
def reverse_each_word(self, l: list) -> None:
n = len(l)
start = end = 0
# 2. Go through the input List
while start < n:
# 3. Determine where the word end - find each word in the input List
while end < n and l[end] != ' ':
end += 1
# reverse the word
self.reverse(l, start, end - 1)
# move to the next word
start = end + 1
end += 1
class Solution {
public void reverseWords(char[] s) {
// 1. Reverse the whole input List in place
reverse(s, 0, s.length - 1);
// 4. Reverse Each Word in the input List in place
reverseEachWord(s);
}
public void reverse(char[] s, int left, int right) {
while (left < right) {
char tmp = s[left];
s[left++] = s[right];
s[right--] = tmp;
}
}
public void reverseEachWord(char[] s) {
int n = s.length;
int start = 0, end = 0;
// 2. Go through the input List
while (start < n) {
// 3. Determine where the word end - find each word in the input List
while (end < n && s[end] != ' ') ++end;
// reverse the word
reverse(s, start, end - 1);
// move to the next word
start = end + 1;
++end;
}
}
}
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the number of items in the array.