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?
Be sure that you clarify the input and output parameters of the problem:
Run through a set of example cases:
HAPPY CASE
Input: m = 3, n = 5, head = [3,0,2,6,8,1,7,9,4,2,5,5,0]
Output: [[3,0,2,6,8],[5,0,-1,-1,1],[5,2,4,9,7]]
Input: m = 1, n = 4, head = [0,1,2]
Output: [[0,1,2,-1]]
EDGE CASE
Input: m = 1, n = 1, head = [0]
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.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Initialize the answer set with all elements as -1. If end of link list is reached, return the answer set else update the current index value to node's current value
1. Try to go up from current index
if you can go left as well from current index, go left
add the left index to visited and call method again
from left cell
else go up
add the up index to visited
call method again from upward cell
2. Try to go right from current index
if possible, add the index to visited and call method again
from the right cell
3. Try to go bottom from current index
if possible, add the index to visited and call method again
from bottom cell
4. Try to go left from current index
if possible, add the index to visited and call method again
from left cell
⚠️ Common Mistakes
Implement the code to solve the algorithm.
class Solution:
def spiralMatrix(self, m: int, n: int, head: Optional[ListNode]) -> List[List[int]]:
ans = [[-1 for _ in range(n)] for _ in range(m)]
visited = set()
def is_valid(i, j):
if 0 <= i < m and 0 <= j < n:
return True
return False
def fun(i, j, head):
if head is None:
return ans
ans[i][j] = head.val
if is_valid(i-1, j) and (i-1, j) not in visited:
if is_valid(i, j-1) and(i, j-1) not in visited:
visited.add((i, j-1))
return fun(i, j-1, head.next)
else:
visited.add((i-1, j))
return fun(i-1, j, head.next)
if is_valid(i, j+1) and (i, j+1) not in visited:
visited.add((i,j+1))
return fun(i, j+1, head.next)
if is_valid(i+1,j) and (i+1, j) not in visited:
visited.add((i+1, j))
return fun(i+1, j, head.next)
if is_valid(i, j-1) and (i, j-1) not in visited:
visited.add((i, j-1))
return fun(i, j-1, head.next)
return ans
visited.add((0,0))
return fun(0, 0, head)
class Solution {
// Add a instance variable to have helper method to fetch value.
ListNode node;
public int[][] spiralMatrix(int m, int n, ListNode head) {
node = head; // point to the head
// new array
int [][]A = new int[m][n];
// define boundaries for spiral
int left = 0, top = 0;
int right = n-1, bottom = m-1;
// variable to keep track of boxes filled
int count = 0, maxCount = m * n;
while (true) {
// left to right
for (int i = left; i <= right; i++) {
A[top][i] = getNext();
count++;
}
top++; // top moved down, as we have filled the top while progressing left to right.
if (count >= maxCount) break; // if all boxes filled, we are done.
// top to bottom
for (int j = top; j <= bottom; j++) {
A[j][right] = getNext();
count++;
}
right--; // right shrinks towards left, as we filled the current right.
if (count >= maxCount) break;
// right to left
for (int k = right; k >= left; k--) {
A[bottom][k] = getNext();
count++;
}
bottom--; // bottom moves up, as we filled the bottom row.
if (count >= maxCount) break; // if all boxes filled, we are done.
// bottom to top
for (int l = bottom; l >= top; l--) {
A[l][left] = getNext();
count++;
}
left++; // left shrinks towards right, we filled the current left.
if (count >= maxCount) break; // if all boxes filled, we are done.
}
return A;
}
// helper method keep returning elements from linkedList, once list is exhausted, it starts returning -1.
int getNext() {
if (node == null) return -1;
int val = node.val;
node = node.next;
return val;
}
}
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.