Checkpoint
Two pointers and sliding windows
These questions mix all four lessons: pointers from both ends, read and write pointers, fixed windows, and windows that grow and shrink. Every input is new, so work each one on paper first: write down where the pointers sit and what the notebook holds after each step, then answer. The last two ask for code: a follow-up to the read and write lesson, and a window with a rule you have not seen.
- Question 1 of 12.
The list is sorted from smallest to largest. This is the sorted two sum walk with one
printadded before each decision. Type exactly what it prints.def pick_two(nums, target): left = 0 right = len(nums) - 1 while left < right: total = nums[left] + nums[right] print(nums[left], nums[right], total) if total == target: return [left, right] if total < target: left += 1 else: right -= 1 return None print(pick_two([1, 3, 4, 7, 9], 11)) - Question 2 of 12.
This is the read and write walk with a new keep rule: keep every number that is not 0. It prints
write, then the whole list. Type exactly what it prints.nums = [0, 7, 0, 0, 3, 9] write = 0 for read in range(len(nums)): if nums[read] != 0: nums[write] = nums[read] write += 1 print(write, nums) - Question 3 of 12.
This fixed-size window keeps the best sum and the position where the best window starts. Some numbers are negative. Type exactly what it prints.
nums = [4, -1, 2, 6, -3, 5] k = 3 window_sum = sum(nums[:k]) best = window_sum best_start = 0 for right in range(k, len(nums)): window_sum += nums[right] - nums[right - k] if window_sum > best: best = window_sum best_start = right - k + 1 print(best, best_start) - Question 4 of 12.
Walls of heights
[3, 1, 6, 4, 5, 2]stand one unit apart. Two walls hold water up to the shorter one, so the amount is the width times the shorter height. Run the shorter-wall walk by hand: start at the two ends, measure, and move the pointer at the shorter wall. What is the most water any two walls hold?most waterType a number: 12, -1, 0.5 and 3/4 all work.
- Question 5 of 12.
A window of k numbers slides one step at a time: one number enters and one leaves. Which notebooks can each slide update in one step, using only the number entering and the number leaving? Choose every one that works.
Choose every option that applies.
- Question 6 of 12.
Someone runs the sorted two sum walk on
nums = [4, 1, 5, 2]with target 3, a list that is not sorted. The walk adds 4 + 2, then 4 + 5, then 4 + 1. Each sum is too big, sorightmoves down each time, and the walk returnsNone. But 1 + 2 = 3. Why did it miss the pair? - Question 7 of 12.
Merge from the back with
nums1 = [4, 5, 9, 0, 0, 0](m = 3; the last three slots are free room) andnums2 = [1, 2, 6](n = 3). The first loop runs while both lists have values waiting, and each step puts the larger waiting value in the slot atwrite. The second loop copies whatever is left ofnums2. How many values does the second loop copy?values copiedType a number: 12, -1, 0.5 and 3/4 all work.
- Question 8 of 12.
This version of the longest stretch without a repeat keeps a dict
lastfrom each character to the position where it was last seen, and jumpsleftpast the old copy in one step. It leaves out themaxthat the lesson warned about. Type exactly what it prints.def longest_unique(s): last = {} left = 0 best = 0 for right, ch in enumerate(s): if ch in last: left = last[ch] + 1 last[ch] = right best = max(best, right - left + 1) return best print(longest_unique("abcbad")) - Question 9 of 12.
Three sum sorts the list, which costs O(n log n). Then, for each of up to n choices of the smallest number, it runs one two-pointer walk that costs O(n). What is the total time?
- Question 10 of 12.
A teammate looks at the variable window template, where a
whileloop that movesleftsits inside aforloop that movesright, and says: "Two nested loops, so this is O(n2)." Which reply is right? - Question 11 of 12.
Run the tests before you submit. This question counts as correct if your latest run passes every check.
Code itKeep each value at most twice numsis sorted. Writekeep_two(nums), which changesnumsin place so that each value appears at most twice, in the original order, and returns how many items are kept. Those items arenums[:k], wherekis the returned count; the slots after them do not matter. For[1, 1, 1, 2, 2, 2, 3]the answer is 5, andnums[:5]is[1, 1, 2, 2, 3].This is the follow-up the read and write lesson predicted: "now each value may stay twice". The tests include a list of 600,000 numbers, where deleting items one at a time is too slow.
Write your code where the starter saysraise NotImplementedError, then press Run tests. Each check says what it expects. - Question 12 of 12.
Run the tests before you submit. This question counts as correct if your latest run passes every check.
Code itFlip a few zeros bitsis a list of 0s and 1s, and you may flip at mostkof the 0s into 1s. Writelongest_ones(bits, k), which returns the length of the longest block of 1s you can end up with. For[1, 0, 1, 1, 0, 0, 1]andk = 1the answer is 4: flip the 0 at position 1, and positions 0 to 3 are all 1s.Use a variable window. The rule it keeps is "at most
kzeros inside", and the notebook counts the zeros. The tests include a list of 50,000 bits, where trying every block is too slow.Write your code where the starter saysraise NotImplementedError, then press Run tests. Each check says what it expects.