educative.io

Question about Circular array

How does this example consider this array not circular?
In here 1,-1 are forming a cycle right? Because from position 1, you go forward 1 step to position 2 (-1) and then backward 1 step to position 1. Why isn’t it considered a cycle?


Course: Grokking Coding Interview Patterns in Java - Learn Interactively
Lesson: Circular Array Loop - Grokking Coding Interview Patterns in Java

I think its not a cycle because of hte constraint “The loop must be in a single direction, forward or backward”

Hello @Kasary_Tran,

Thank you for your feedback. As mentioned by @Geetikaarora, the cycle doesn’t exist because of the change in direction of the loop. The negative value reverses the direction at index 2 which then violates the constraint mentioned in the problem statement.

If you try out the test case [1, 1], you’ll see that it would return True, this is because the direction of the loop is forward only, whereas if you test [1, -1], the function would return False, indicating that a cycle doesn’t exist.

Hope this answers your query. Feel free to share more feedbacks and suggestions.

Thanks and regards,
Dian Us Suqlain
Developer Advocate @Educative.io