Amazon Online Assessment (OA) - Count Rotations

A sorted list of unique integers is rotated clockwise k times. The function count_rotations() is given the rotated list rlist and size, the size of rlist, and is expected to return the value of k.

Examples

Example 1:

Input:

rlist = [100, 12, 50]

Output: 1
Explanation:

The original sorted list [12, 50, 100] was rotated clockwise 1 time to become [100, 12, 50].

count_rotations() calls another function count_rotations_helper() which takes in 3 parameters:

  • rlist: a rotated sorted list of unique integers,
  • head: the index of the first value of the sorted list,
  • end: the index of the last value of the sorted list. The helper function computes the value k and passes the result back to count_rotations().

The current implementation of the functions compiles successfully but does not return the correct result consistently because of some error in count_rotations_helper(). Please fix the code so that it passes all test cases.

Try it yourself

Solution

Title

Script

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Contrary to popular belief, Lorem Ipsum is not simply random text.

  >>> a = [1, 2, 3]
  >>> a[-1]
  3

Get premium for instant access to all content and solutions

Upgrade