Sort products by price and attention score
Company: Coupang
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Onsite
Quick Answer: This question evaluates the ability to parse and sanitize input strings, implement multi-key sorting with numeric and lexicographic tie-breakers, and analyze time and space complexity while correctly handling malformed records.
Constraints
- 0 <= len(entries) <= 100000
- Each entry has length at most 1000
- A valid entry must have exactly three comma-separated fields
- price and attention must be valid integers
- Each valid record contributes one product name to the output, even if product names are duplicated
Examples
Input: (["phone,699,80", "case,19,40", "charger,29,70", "cable,9,70"],)
Expected Output: ["cable", "case", "charger", "phone"]
Explanation: The records are sorted by increasing price: cable costs 9, case 19, charger 29, and phone 699.
Input: (["alpha,10,5", "beta,10,8", "gamma,10,8", "delta,5,1"],)
Expected Output: ["delta", "beta", "gamma", "alpha"]
Explanation: delta has the lowest price. Among the price-10 products, beta and gamma have attention 8 and come before alpha with attention 5. beta comes before gamma lexicographically.
Input: (["valid,20,2", "badprice,xx,10", "missing,15", "badatt,30,nope", "also valid,10,5", "extra,1,2,3"],)
Expected Output: ["also valid", "valid"]
Explanation: Only 'valid,20,2' and 'also valid,10,5' are valid records. The other entries are ignored because they have missing fields, extra fields, or non-numeric values.
Input: ([],)
Expected Output: []
Explanation: There are no entries to parse, so the result is an empty list.
Input: ([" zeta ,0,0", "eta,0,10", "theta,0,10", "iota,0,-1"],)
Expected Output: ["eta", "theta", "zeta", "iota"]
Explanation: All prices are equal, so attention descending is used. eta and theta both have attention 10, so they are ordered lexicographically. Whitespace around zeta is ignored.
Input: (["apple,5,5", "apple,3,1", "banana,3,1"],)
Expected Output: ["apple", "banana", "apple"]
Explanation: Duplicate product names are allowed. The two price-3 records come first and are ordered lexicographically by name, followed by the price-5 apple record.
Solution
def solution(entries):
records = []
for entry in entries:
parts = entry.split(',')
if len(parts) != 3:
continue
name = parts[0].strip()
price_text = parts[1].strip()
attention_text = parts[2].strip()
try:
price = int(price_text)
attention = int(attention_text)
except ValueError:
continue
records.append((price, -attention, name))
records.sort()
return [name for price, neg_attention, name in records]Time complexity: O(n log n), where n is the number of input entries. Parsing is O(n) and sorting the valid records dominates.. Space complexity: O(n), for storing the parsed valid records before sorting..
Hints
- After parsing a valid record, consider storing a tuple that represents the desired sort order.
- Descending attention can be handled in an ascending sort by using the negative of the attention score.