← Problems88. Standardise free-text citiesEasyPySpark
00:00 / 15:00

Standardise free-text cities

Easy100% solve itAsked at Deloitte, TCS

City and country were typed into a web form with no validation, so the same place is in the table a dozen ways. Normalise them before anybody groups by them.

Input

contacts contact_id bigint raw_city string -- free text; may be null, blank, or padded country string -- a two-letter code, in any case; may be null

Output

Every input row, with the columns:

  • contact_id
  • city — trimmed, every run of whitespace reduced to one space, then title case. 'UNKNOWN' if it is null or blank.
  • country — trimmed and upper case. 'UNKNOWN' if it is null or blank.

Example

contact_idraw_citycountrycitycountry
1 new delhi inNew DelhiIN
3 usUNKNOWNUS
4AustinnullAustinUNKNOWN

Constraints

  • Whitespace includes tabs and newlines, and the runs that matter are the ones inside the value — trim alone does not reach those.
  • BENGALURU, bengaluru and BeNgAlUrU must all come out as Bengaluru.
  • Blank is not the same as null, and both become 'UNKNOWN'. A comparison against '' on a null column is null, not false, so the null check has to happen too.
  • Row order is not graded. Column names and their order are.

Example 1padding and a run of spaces inside the name

Input · contacts
countryraw_citycontact_id
in new delhi 1
INPune2
Output
contact_idcitycountry
1New DelhiIN
2PuneIN

Example 2blank and missing both become UNKNOWN

Input · contacts
countryraw_citycontact_id
us 3
nullAustin4
Output
contact_idcitycountry
3UNKNOWNUS
4AustinUNKNOWN

Topics

data quality

Similar problems

Community-reported interview topic. Not an official company question and no affiliation is implied.

solution.py
Loading editor…
Draft not saved yet · Spaces 4 · UTF-8⌘↵ run ⌘⇧↵ submit
Nothing run yet

Run against the public tests, or submit to score against all of them.