There are two ways to implement a circular array either take a new array of size 2N where N is the size of the initial array and copy initial array twice into that new array

array = [1,2,3,4,5]
new_array = [1,2,3,4,5,1,2,3,4,5]

Or we can use (i % N) where N is the size of the array, i is the index of that element

for(var i = 0; i < N + i; i++)

What does exactly circular array mean? Is a normal array while representing?


Circular array is just a fixed size array. We call it circular just because we define the operations on the array in such a manner that its limited size can be utilized again and again and it apparently appears that we are traversing the array in clockwise or anti-clockwise way.

The best example for understanding the circular array is a Queue. We can implement Queue with the help of a circular array. In a Queue we insert at the end and delete from the front.

Suppose you have an array of size 5. Let say we implement Queue using this array. We will have two pointers - one for the front of Queue and one for the rear of Queue. Initially front and rear both will point at index 1. We may do let's say 3 insertions in the beginning. Rear pointer would point at index 3 and front would point index 1 at this moment (consider 1-based indexing).

Now let say we make 2 deletions and now front is 3 and rear is 3. At this moment we have 2 array positions vacant in the front of array. We can use this space again rather than reallocating an array once rear reaches the end of the array. What we can do to utilize these spaces again, is to introduce the circularity, which when rear or front reaches the end of the array re-indexes the rear to 1th index. Since we have changed the index from last index to the first index, we say that the array is circular (apparently in a clockwise manner).

The idea to re-allocate an array and copy the original array twice is totally wrong. The % operation lets you re-index the pointer once you have reached the end of array (e.g. 6%5 = 1). The approach with the mod operation is the correct approach to implement a circular array.


I have added an image which will help you:


  • $\begingroup$ what if there's no pointer and we are dealing with arrays in Javascript/Ruby/Python? $\endgroup$ – Abhimanyu Aryan Sep 17 '18 at 17:38
  • $\begingroup$ ohh that way got it. Whenever I see pointers. I get confused with memory locations. Thanks $\endgroup$ – Abhimanyu Aryan Sep 17 '18 at 17:41
  • 1
    $\begingroup$ What have you understood by pointer? By pointer i just meant a variable which holds the value of a certain index in array. I didn't mean the pointers as in C language. $\endgroup$ – Navjot Waraich Sep 17 '18 at 17:41
  • $\begingroup$ Also do accept the answer if you find it useful. $\endgroup$ – Navjot Waraich Sep 17 '18 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.