Problem Β· String

HTTP Accept-Language Matching (Exact, Prefix, Wildcard)

Learn this problem
● MediumStripe logoStripeFULLTIMEPHONE SCREEN
See Stripe hiring insights

Problem statement

Practice sequence: Exact, prefix, and wildcard matching β†’ Quality scores (q-factors).

You serve localized content by resolving a browser's HTTP Accept-Language header against the languages your server supports. The header lists language tags in order of preference, separated by commas, with optional surrounding spaces.

Given acceptHeader and supportedLanguages, return the supported tags that work for the request in descending client preference order. Apply these source-defined parts as you scan the header left to right:

  1. Part 1 β€” exact match: a tag such as en-US matches the same supported tag.
  2. Part 2 β€” generic language: a region-less tag such as en matches supported variants that start with en-.
  3. Part 3 β€” wildcard: * matches every supported tag not already selected.

When one header entry matches several supported languages, keep their order from supportedLanguages. Never return a supported tag more than once.

Function

parseAcceptLanguage(acceptHeader: String, supportedLanguages: String[]) β†’ String[]

Examples

Example 1

acceptHeader = "fr-FR, fr"supportedLanguages = ["en-US", "fr-CA", "fr-FR"]return = ["fr-FR", "fr-CA"]
fr-FR is an exact match and comes first. Then the generic fr matches the remaining French variant fr-CA (fr-FR is skipped because it is already used). en-US is never requested.

Example 2

acceptHeader = "fr-FR, fr, *"supportedLanguages = ["en-US", "fr-CA", "fr-FR"]return = ["fr-FR", "fr-CA", "en-US"]
fr-FR matches exactly. fr matches fr-CA. The wildcard * then grabs the last unmatched supported tag, en-US.

Example 3

acceptHeader = "en-US, fr-CA, fr-FR"supportedLanguages = ["fr-FR", "en-US"]return = ["en-US", "fr-FR"]
en-US is first in the header and supported. fr-CA is not supported. fr-FR is third in the header and supported. Order follows the header preference.

Constraints

  • acceptHeader is a comma-separated list of tags with optional surrounding spaces.
  • Exact matches use the full tag; a region-less generic tag matches every supported tag starting with generic-.
  • * matches all not-yet-matched supported tags, in supportedLanguages order.
  • Preserve the header's preference order; never output a supported tag twice.
  • Return the matched supported tags as a String[].

More Stripe problems

drafts saved locally
public String[] parseAcceptLanguage(String acceptHeader, String[] supportedLanguages) {
  // write your code here
}
acceptHeader"fr-FR, fr"
supportedLanguages["en-US", "fr-CA", "fr-FR"]
expected["fr-FR", "fr-CA"]
checking account