PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • Medium
  • Coupang
  • Coding & Algorithms
  • Software Engineer

Sort products by price and attention score

Company: Coupang

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Onsite

You are given a list of strings, each formatted as "product,price,attention". Parse the list into records and return the product names sorted to prioritize low price and high attention: sort by price ascending, then by attention descending, and finally by product name lexicographically to break any remaining ties. Assume price and attention are integers. Ignore malformed entries (missing fields or non-numeric price/attention). Describe your approach, data structures used, time and space complexity, and provide working code.

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.

You are given a list of strings where each string should represent one product record in the format "product,price,attention". Parse the valid records and return only the product names sorted with the following priority: price ascending, attention descending, and product name lexicographically ascending to break remaining ties. Price and attention are integers. Ignore malformed entries, including entries that do not have exactly three comma-separated fields or entries whose price or attention cannot be parsed as an integer. Whitespace around fields should be ignored. Approach: scan the input once, parse valid records into tuples, then sort using a tuple key of (price, -attention, product_name).

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

  1. After parsing a valid record, consider storing a tuple that represents the desired sort order.
  2. Descending attention can be handled in an ascending sort by using the negative of the attention score.
Last updated: Jun 14, 2026

Related Coding Questions

  • Find shortest distance between two islands - Coupang (medium)
  • Compute minimal flips connecting two islands - Coupang (Medium)

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.