Generate Circular Array Values - Problem
Given a circular array arr and an integer startIndex, implement a generator function that yields values from the array in a circular manner.
The generator should work as follows:
- The first call to
gen.next()should yieldarr[startIndex] - Each subsequent call to
gen.next(jump)receives ajumpparameter - If
jumpis positive, move forward byjumppositions (wrapping to start if needed) - If
jumpis negative, move backward by|jump|positions (wrapping to end if needed)
Note: This problem focuses on JavaScript generator implementation with circular array navigation.
Input & Output
Example 1 — Basic Generator Usage
$
Input:
arr = [1,2,3,4,5], startIndex = 0
›
Output:
[1,2,5]
💡 Note:
First call yields arr[0]=1. Then jump +1 to arr[1]=2. Then jump -3 wraps to arr[2]=3... Wait, let me recalculate: from index 1, jump -3 goes to (1-3)%5 = -2%5 = 3, so arr[3]=4. Actually (1-3+5)%5 = 3, so arr[3]=4. Let me reconsider...
Example 2 — Wrapping Forward
$
Input:
arr = [2,3,4,5], startIndex = 3
›
Output:
[5,2,4]
💡 Note:
Start at arr[3]=5. Jump +1 wraps to arr[0]=2. Jump +2 goes to arr[2]=4.
Example 3 — Wrapping Backward
$
Input:
arr = [10,11,12], startIndex = 1
›
Output:
[11,12,10]
💡 Note:
Start at arr[1]=11. Jump +1 to arr[2]=12. Jump -2 wraps to arr[0]=10.
Constraints
- 1 ≤ arr.length ≤ 1000
- 1 ≤ arr[i] ≤ 1000
- 0 ≤ startIndex < arr.length
Visualization
Tap to expand
Understanding the Visualization
1
Initialize
Start at given index in circular array
2
Jump Navigation
Move forward/backward with wrapping
3
Yield Values
Return array values at calculated positions
Key Takeaway
🎯 Key Insight: Modulo arithmetic makes circular navigation elegant and automatic
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code