bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch
🌱Newbie
0 XP
Back to starter track
🎯Code CheckjavascriptEasySliding Window

Starter: return the result

Why should this code return the variable total? Goal: Use the value that was already calculated instead of recalculating it.

Starter step 2 of 20Read code one line at a time

Follow a tiny function from setup to return.

Look for the value that is updated every loop and then returned.

Problem Brief

You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.

Puzzle Hints
  1. Why should this code return the variable total?

  2. The return line should use the value the code just calculated. The right choice is "total already stores the result of 2 + 3" because it preserves the goal: Use the value that was already calculated instead of recalculating it.

  3. Tie your answer back to Sliding Window: what data is stored, when it updates, and what condition uses it?

Asked at 38 companies
AdobeAkuna CapitalAlibaba
Best Time to Buy/Sell — Sliding Min
two pointers
buysell701152336445
buy=0
sell=1
1
6

Track minimum buy price and maximum profit

How to think: Sliding Windowguide

You need the longest/shortest subarray or substring that satisfies a condition.

1.Ask: "Am I looking for a contiguous subarray/substring?" → Sliding window
2.Start: expand the right pointer to grow the window
3.When the window violates the condition: shrink from the left
4.Track your answer (max/min window size) at each valid state
5.Key: the window only grows right or shrinks left — never backtracks

vs Brute force (check all subarrays): You're looking at contiguous sequences — window avoids recomputing from scratch

vs Two Pointers: You need a range/subarray (not just two specific elements)

longest substringshortest subarraycontiguouswindow of size kat most k distinct
How to think: Sliding Windowguide

You need the longest/shortest subarray or substring that satisfies a condition.

1.Ask: "Am I looking for a contiguous subarray/substring?" → Sliding window
2.Start: expand the right pointer to grow the window
3.When the window violates the condition: shrink from the left
4.Track your answer (max/min window size) at each valid state
5.Key: the window only grows right or shrinks left — never backtracks

vs Brute force (check all subarrays): You're looking at contiguous sequences — window avoids recomputing from scratch

vs Two Pointers: You need a range/subarray (not just two specific elements)

longest substringshortest subarraycontiguouswindow of size kat most k distinct
1// Goal: Use the value that was already calculated instead of recalculating it.
2function starterExample() {
3 const total = 2 + 3;
4 return total;
5}

Why should this code return the variable total?