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
- Secure Maximum DeliveriesOA Β· Seen Jul 2026
- Find Median from Data StreamONSITE INTERVIEW Β· Seen Jul 2026
- Handwritten SigmoidPHONE SCREEN Β· Seen Jul 2026
- Handwritten SoftmaxPHONE SCREEN Β· Seen Jul 2026
- Koko Eating BananasONSITE INTERVIEW Β· Seen Jul 2026
- Loyal Customers Across Two DaysONSITE INTERVIEW Β· Seen Jul 2026
- Maximum System Memory CapacityOA Β· Seen Jul 2026
- Package Delivery SystemOA Β· Seen Jul 2026