AMD Interview Question
Computer ScientistsCountry: United States
Note the value of the start node. Iterate through the nodes around the cycle forward. On the nth iteration, switch the value of the nth next node if it has the same value as the start node. Go back and visit the first node again and see if its value has changed. If it has, there are n nodes.
If you can keep a pointer to your start node, just flip the values and check the value of the start node after each flip.
I don't see how you can do it without storing some form of state.
Should I assume that I can't use any other variable other the count of the elements?
- rasimu February 23, 2017