Unit 4 Session 2 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Implement a sliding window to count negative numbers within each window of size k in the list.
1) Initialize a list `result` to store the count of negative numbers for each window.
2) Use a pointer `start` to denote the beginning of each window.
3) Initialize a variable `count` to keep track of the number of negative numbers in the current window.
4) Iterate over the list using an index `i`:
a) If the current number is negative, increase the `count`.
b) When the window size reaches k (i.e., i - start + 1 == k):
i) Append `count` to `result`.
ii) If the number at the `start` of the window is negative, decrease the `count`.
iii) Increment `start` to slide the window right.
5) Return the `result` list.
⚠️ Common Mistakes
start
index, leading to windows of incorrect size.def count_negatives(lst, k):
result = []
start = 0
count = 0
for i in range(len(lst)):
if lst[i] < 0:
count += 1
if (i - start + 1 == k):
result.append(count)
if lst[start] < 0:
count -= 1
start += 1
return result