Python Interview Prep/Which Language to Serve
Stripe logo

Which Language to Serve

Stripe·medium·Engineering·About 25 min in the room

A browser sends an Accept-Language header such as "en-US, fr-CA, fr-FR": tags in order of preference, separated by commas. Your server supports a list of tags such as ["fr-FR", "en-GB", "en-US"]. Write parse_accept_language(header, supported) returning the supported tags the client accepts, in the client's order of preference, each at most once. A header tag matches in one of three ways. Exactly: en-US matches en-US. As a bare language: en matches every supported en- tag, in the order they appear in supported. Or as *, which matches every supported tag not already returned, in supported order. Matching ignores case, but return each tag exactly as supported writes it.

Examples

parse_accept_language("en-US, fr-CA, fr-FR", ["fr-FR", "en-GB", "en-US"])
['en-US', 'fr-FR']
parse_accept_language("fr", ["en-US", "fr-CA", "fr-FR"])
['fr-CA', 'fr-FR']
parse_accept_language("fr-FR, *", ["en-US", "fr-CA", "fr-FR"])
['fr-FR', 'en-US', 'fr-CA']

What this question tests

Modelled on a multi-part question widely reported from Stripe phone screens: parse an HTTP Accept-Language header against the locales a server supports. It tests careful parsing and keeping two orders straight at once, the client's preference and the server's list.

More Python interview questions

  • The Rate-Limited Logger · Google · easy
  • Paginate the Search Results · Airbnb · hard
  • Find the Duplicate Files · Dropbox · medium
  • A Cache That Forgets · Microsoft · medium
Python
Shift+EnterPython is starting in your browser...
Output

Starting Python...