Code Logo

Circular Queue Simulation

Published at25 Jul 2026
Easy 0 views
Like0

Create an array of size n filled with values 0 to n-1, simulating a circular queue initialization.

Example Input & Output

Example 1
Input
0
Output
[]
Explanation

Empty

Example 2
Input
5
Output
[0,1,2,3,4]
Explanation

Size 5

Example 3
Input
3
Output
[0,1,2]
Explanation

Circular queue of size 3 with values 0..2

Example 4
Input
2
Output
[0,1]
Explanation

Size 2

Example 5
Input
1
Output
[0]
Explanation

Size 1

Algorithm Flow

Recommendation Algorithm Flow for Circular Queue Simulation
Recommendation Algorithm Flow for Circular Queue Simulation

Solution Approach

function solution(n) {
  var r = [];
  for (var i = 0; i < n; i++) r.push(i);
  return r;
}

Best Answers

java
class Solution {
    public int[] solution(int n) {
        int[] r=new int[n];
        for(int i=0;i<n;i++)r[i]=i;
        return r;
    }
}