Uber OA Coding Interview: Audiobook Listening with Cyclic Order and Drops

18 Views
No Comments

You have an array of integers audiobooks where audiobooks[i] is the amount of time in minutes needed to listen to the i-th audiobook fully. You are listening to the audiobooks in a cyclic order:

  • For the first time, you pick the audiobook at index 0 on your list.
  • The next time, you pick the audiobook at index 1 on your list.
  • The next time, you pick the audiobook at index 2 on your list.
  • After picking the last audiobook on your list, you return to index 0 again.

Also, you have a list of event logs. An event can be:

  • "LISTEN <minutes>" — pick the next audiobook following the cyclic order described above and listen to it for <minutes> time if you have not finished that audiobook yet. If you complete the book, it should stay on the list, so audiobook indices stay the same.
  • "DROP <i>" — you lose interest in the audiobook at index i and decide not to listen to it in the future. The audiobook is not removed from the list, so all indices stay the same.

Dropping an audiobook does not affect the listening order. For the next LISTEN operation, continue after the audiobook you listened to last.

Return the index of the audiobook with the greatest total listening time after processing all events. If there is a tie, return the largest index.

This Uber OA problem is a simulation of cyclic audiobook listening with dropped items. You need to track the current position in the circular list, skip dropped audiobooks, and accumulate listening time for each index as LISTEN events arrive. Since dropped books are not removed, the indices remain stable, and the main challenge is keeping the traversal order correct across multiple events. After processing all logs, return the index with the largest total listening time, breaking ties by choosing the larger index.

END
 0