AMAZON Coding Question – Solved

9 Live
Amazon Books is a retail store that sells the newly launched novel "The Story of Amazon". The novel is divided into n volumes numbered from 1 to n and unfortunately, all the volumes are currently out of stock. The Amazon team announced that starting today, they will bring exactly one volume of "The Story of Amazon" in stock for each of the next n days. On the nth day, all volumes will be in stock. Being an impatient bookworm, each day you will purchase the maximum number of volumes you can such that: - You have not purchased the volume before. - You already own all prequels (prior volumes). Note: For the ith volume of the novel, all the volumes j such that j < i are its prequels. Determine the volumes you would purchase each day. You should return an array of n arrays where the ith array contains: - The volume numbers sorted in increasing order if you purchased some volumes on the ith day. - The single element -1 if you did not purchase any book. Example: volumes = [2, 1, 4, 3] Initially, all volumes are out of stock: - On the first day, Volume No. 2 becomes available. Since you do not own its prequel (Volume 1) yet, you will not purchase it. The answer for the first day is [-1]. - On the second day, Volume No. 1 becomes available. Now, you can purchase both volumes 1 and 2 together. The answer for the second day is [1, 2]. - On the third day, Volume No. 4 becomes available. Since you do not have one of its prequels (Volume 3) yet, you will not buy Volume 4. The answer for the third day is [-1]. - On the fourth day, Volume No. 3 becomes available. Now, you can purchase both volumes 3 and 4. The answer for the fourth day is [3, 4]. The final answer is: [[-1], [1, 2], [-1], [3, 4]] Function Description: Complete the function orderBooks in the editor below. orderBooks has the following parameter: int volumes[n]: an array of integers where the ith integer denotes the volume that is in stock on the ith day Returns: int[][]: a 2D array of integers where the ith array denotes the volumes purchased on the ith day.

Asked in: AMAZON

Image of the Question

Question Image Question Image Question Image Question Image Question Image

All Testcases Passed βœ”



Passcode Image

Solution


Please login to view the solution


Related Questions

| As an operations engineer at Amazon, you are responsible for organizing the dis… |
| In an Amazon analytics team, the Analysts collectively have a preference for th… |
| AWS provides scalable systems. A set of n servers are used for horizontally sca… |
| A foundry in Hackerland makes an alloy out of n different metals. In the manufa… |
| An array of integers is almost sorted if at most one element can be deleted fro… |
| The binary cardinality of a number is the count of 1's in its binary representa… |