Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
1,124 views

I have recently completed the OA provided by Amazon and below is the one of the question:

 

Amazon organized a movie festival to promote its online video- streaming platform, amazon prime video.
In the festival, movies of 2 categories, "comedy" and "drama" were played on a loop. The original sequence of movies is represented as a binary string, where a comedy movie is denoted by '0', and a drama is '1'. The movies are on a continuous loop, so if the original sequence of movies is "01101" and is concatenated infinitely so they are played in the order "011010110101101. . . . ".
The popularity of the festival is the number of times the difference between the number of comedies and dramas played so far (that is the prefix till that index) is exactly equal to diff. More formally, the popularity of the festival is the number of prefixes in the concatenated string for which count('0') - count('1') = diff.

 

Given the original sequence of movies denoted by movieorder, and the popularity parameter diff, find the popularity of the festival. If there are infinite such instances possible, return "-1". If there is no such instance possible, return "0".

 

Notes:

 

  • a binary string consists of characters '0'and '1
  • empty strings are also considered.

 

Example

 

  1. movieorder="110100" and diff=-1, output:-1
  2. movieorder="10100"and dif= 2,output: 5
  3. movieorder="110000"and diff= -2,output: 1

 

Constraints

 

  • 1<=|movieOrders|<= 2*10^5
  • -10^9<= diff<= 10^9
  • The string movieOrder consists of only O's and 1's
in Online Assessments by Expert (108,690 points)
edited by | 1,124 views

Please log in or register to answer this question.