Problem · String

HTTP Accept-Language with Quality Scores (q-factors)

MediumStripeONSITE INTERVIEW
See Stripe hiring insights

This continues the HTTP Accept-Language matcher. Now each requested tag may carry a quality factor in the form tag;q=value, where value is between 0.0 and 1.0 and expresses how strongly the user wants that tag.

Matching against supportedLanguages still uses exact, prefix (generic, no region matches generic-*), and wildcard (*) rules, and each matched supported tag inherits the quality score of the header entry that matched it. Rules for the score:

  • If a tag has no ;q=, its quality defaults to 1.0.
  • A higher score means a stronger preference.
  • The wildcard * may also carry a score.

Return the matched supported tags sorted by quality score descending. When scores tie, keep the order in which the tags were matched (stable sort). Entries with q=0 sort last (lowest preference). Return the tag names only, as a String[].

Examples
01 · Example 1
acceptHeader = "fr-FR;q=1, fr-CA;q=0, fr;q=0.5"
supportedLanguages = ["fr-FR", "fr-CA", "fr-BG"]
return = ["fr-FR", "fr-BG", "fr-CA"]
fr-FR matches exactly with q=1.0. The generic fr (q=0.5) matches the remaining supported French variant fr-BG. fr-CA matches exactly with q=0. Sorting by score descending: fr-FR (1.0), fr-BG (0.5), fr-CA (0).
02 · Example 2
acceptHeader = "fr-FR;q=1, fr-CA;q=0, *;q=0.5"
supportedLanguages = ["fr-FR", "fr-CA", "fr-BG", "en-US"]
return = ["fr-FR", "fr-BG", "en-US", "fr-CA"]
fr-FR matches with q=1.0. fr-CA matches with q=0. The wildcard *;q=0.5 matches the remaining supported tags fr-BG and en-US, both with q=0.5. Sorting by score descending and keeping match order for ties: fr-FR (1.0), then fr-BG and en-US (0.5, in match order), then fr-CA (0).
03 · Example 3
acceptHeader = "en;q=0.8, fr;q=0.9, de;q=0.7"
supportedLanguages = ["en-US", "fr-FR", "de-DE"]
return = ["fr-FR", "en-US", "de-DE"]
Generic en matches en-US (q=0.8), fr matches fr-FR (q=0.9), de matches de-DE (q=0.7). Sorted by score descending: fr-FR (0.9), en-US (0.8), de-DE (0.7).
Constraints
  • Each header entry is tag or tag;q=value; missing q defaults to 1.0.
  • Matching uses exact, prefix (generic), and wildcard rules; a matched supported tag takes the matching entry's quality score.
  • Sort matched tags by quality descending; ties keep match order (stable); q=0 sorts last.
  • Never output a supported tag twice.
  • Return the tag names as a String[].
More Stripe problems
drafts saved locally
public String[] parseAcceptLanguageWithQuality(String acceptHeader, String[] supportedLanguages) {
  // write your code here
}
acceptHeader"fr-FR;q=1, fr-CA;q=0, fr;q=0.5"
supportedLanguages["fr-FR", "fr-CA", "fr-BG"]
expected["fr-FR", "fr-BG", "fr-CA"]
sign in to submit