Sort Product Codes π
Learn this problemProblem statement
Amazon has millions of products sold on its e-commerce website, and each product is identified by its product code.
Given an array of n productCodes and order, a string that represents the precedence of characters, sort the productCodes in lexicographically increasing order per the precedence.
Note: Lexicographical order is defined in the following way. When we compare strings s and t, first we find the leftmost position with differing characters: sl and tl. If there is no such position (i.e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters sl and t[i] according to their order in the given precedence order.
Function
sortProductCodes(order: String, productCodes: String[]) β String[]
Complete the function sortProductCodes in the editor below. sortProductCodes has the following parameter(s):
string order: the new precedence order stringproductCodes[n]: the array to sort
Returns
string[n]: the productCodes array in sorted order
Examples
Example 1
order = "abcdefghijklmnopqrstuvwxyz"productCodes = ["adc", "abc"]return = ["abc", "adc"]Constraints
1 ≤ n ≤ 50001 ≤ length(productCodes[i]) ≤ 100More Amazon problems
- Drone Delivery RouteOA Β· Seen Aug 2026
- Package Dependency OrderPHONE SCREEN Β· ONSITE INTERVIEW Β· Seen Aug 2026
- Unfulfilled Customers by Inventory PriorityOA Β· Seen Aug 2026
- Calculate Beauty ValuesOA Β· Seen Aug 2026
- Maximize Distance to the Closest Occupied SeatONSITE INTERVIEW Β· Seen Aug 2026
- Package Delivery SystemOA Β· Seen Aug 2026
- Select Least Resource TasksOA Β· Seen Aug 2026
- Maximum Length-K Window Sum over Sparse SegmentsOA Β· Seen Aug 2026