Skip to main content

Generate Circular Array Values - Solution & Explanation

MediumPremiumFree on FleetCode3 min read
Practice this problem

Problem Statement

Given a circular array arr and an integer startIndex, return a generator object gen that yields values from arr.

The first time gen.next() is called on the generator, it should should yield arr[startIndex].

Each subsequent time gen.next() is called, an integer jump will be passed into the function (Ex: gen.next(-3)).

  • If jump is positive, the index should increase by that value, however if the current index is the last index, it should instead jump to the first index.
  • If jump is negative, the index should decrease by the magnitude of that value, however if the current index is the first index, it should instead jump to the last index.

 

Example 1:

Input: arr = [1,2,3,4,5], steps = [1,2,6], startIndex = 0
Output: [1,2,4,5]
Explanation:  
 const gen = cycleGenerator(arr, startIndex);
 gen.next().value;  // 1, index = startIndex = 0
 gen.next(1).value; // 2, index = 1, 0 -> 1
 gen.next(2).value; // 4, index = 3, 1 -> 2 -> 3
 gen.next(6).value; // 5, index = 4, 3 -> 4 -> 0 -> 1 -> 2 -> 3 -> 4

Example 2:

Input: arr = [10,11,12,13,14,15], steps = [1,4,0,-1,-3], startIndex = 1
Output: [11,12,10,10,15,12]
Explanation: 
 const gen = cycleGenerator(arr, startIndex);
 gen.next().value;   // 11, index = 1
 gen.next(1).value;  // 12, index = 2
 gen.next(4).value;  // 10, index = 0
 gen.next(0).value;  // 10, index = 0
 gen.next(-1).value; // 15, index = 5
 gen.next(-3).value; // 12, index = 2

Example 3:

Input: arr = [2,4,6,7,8,10], steps = [-4,5,-3,10], startIndex = 3
Output: [7,10,8,4,10]
Explanation:  
 const gen = cycleGenerator(arr, startIndex);
 gen.next().value   // 7,  index = 3
 gen.next(-4).value // 10, index = 5
 gen.next(5).value  // 8,  index = 4
 gen.next(-3).value // 4,  index = 1  
 gen.next(10).value // 10, index = 5

 

Constraints:

  • 1 <= arr.length <= 104
  • 1 <= steps.length <= 100
  • -104 <= steps[i], arr[i] <= 104
  • 0 <= startIndex < arr.length

Approach Overview

Problem Overview: You need to generate values from an array while treating the array as circular. When the index reaches the end, it wraps back to the beginning. The task is essentially controlled traversal using modular arithmetic so indices never go out of bounds.

Approach 1: Direct Circular Simulation (O(n) time, O(1) space)

The straightforward method is to simulate the traversal while wrapping indices using modulo. Iterate through the required number of steps and compute the next index with (currentIndex + step) % n. This guarantees the index always stays inside the array range while preserving circular behavior. The key insight is that modulo naturally models circular structures, so no extra data structures are needed. This works well for problems involving arrays and simulation patterns.

Approach 2: Mathematical Index Computation (O(n) time, O(1) space)

Instead of explicitly moving step‑by‑step, you can compute the index for each position directly using a formula derived from modular arithmetic. For position i, compute the corresponding circular index using an expression like (start + i * step) % n. This eliminates intermediate state updates and keeps the implementation concise. The algorithm still processes each required value once, resulting in linear time. This technique appears frequently in problems involving modular arithmetic or cyclic traversal.

Recommended for interviews: The direct circular simulation is typically what interviewers expect first. It clearly demonstrates that you understand circular indexing and modulo behavior. After presenting the simulation, pointing out the mathematical formulation shows deeper reasoning about patterns in cyclic sequences.

Solution

Code

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Direct Circular SimulationO(n)O(1)Best general solution when generating values step-by-step in a circular structure
Mathematical Index ComputationO(n)O(1)When the traversal follows a predictable pattern that can be expressed with modular arithmetic

Video Solution

Product of Array Except Self - Leetcode 238 - Python • NeetCode • 747,485 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Generate Circular Array Values easy or hard?
The problem is typically rated Medium because the core logic is simple but requires recognizing that modulo arithmetic models circular behavior. Once the circular indexing pattern is identified, the implementation becomes straightforward.
Generate Circular Array Values Python/Java solution
Implement the traversal using a loop and modulo indexing. Maintain a current index variable and update it with (index + step) % n each iteration. The same logic works in Python, Java, TypeScript, or C++ with identical O(n) time complexity.
How to solve Generate Circular Array Values in O(n)?
Iterate through the required number of values and compute the next position using modulo arithmetic. For example, update the index using (current + step) % n so the array behaves like a loop. This avoids boundary checks and guarantees linear time traversal.
What is the best approach for Generate Circular Array Values?
The most practical solution uses direct circular simulation with modulo arithmetic. Each step calculates the next index using (index + step) % n so the traversal wraps around the array. This runs in O(n) time and O(1) space because each value is produced once without extra storage.
Is Generate Circular Array Values asked at Google/Amazon/Meta?
Problems involving circular arrays and modulo indexing frequently appear in interviews at companies like Google, Amazon, and Meta. While this exact problem may vary, the underlying concept of circular traversal and modular arithmetic is a common interview pattern.
What data structure is used in Generate Circular Array Values?
The core data structure is a standard array treated as a circular structure. The algorithm relies on modular arithmetic to wrap indices rather than creating a specialized circular buffer or linked structure.
What is the time complexity of Generate Circular Array Values?
The optimal solution runs in O(n) time because the algorithm generates or processes each required value exactly once. Space complexity is O(1) since only a few variables are needed to track the current position and compute the next circular index.

Ready to solve this problem?

Practice Generate Circular Array Values with our built-in code editor and test cases.

Practice on FleetCode