Zone postcode map
MediumOnline Assessment
A courier company assigns each delivery address to a delivery zone using a list of postal-code rules, where rules are allowed to overlap.
Each rule is a string "[!]ZONE:SPEC". ZONE is the zone name, or - meaning "we do not deliver here". SPEC is one of:
- a single code,
"10250"; - an inclusive range,
"10200-10299"; - a wildcard of five characters where trailing
*characters stand for any digit,"102**"is 10200 to 10299 and"1****"is 10000 to 19999.
Postal codes are exactly five digits and compare as numbers (02134 is 2134). A rule matches a code that falls inside its range. When several rules match the same code, the winner is chosen like this:
- A rule starting with
!(an override) beats any rule without one. - Among rules of the same kind, the rule covering fewer codes wins (a single code covers 1,
102**covers 100). - If still tied, the rule that appears earlier in the list wins.
A query is valid if it is five digits ("10250") or five digits, a hyphen and four digits ("10250-1234", a ZIP+4; only the first five digits matter). Anything else is invalid.
Input: rules, a list of rule strings, and queries, a list of strings.
Output: a list with one string per query: "invalid" for an invalid query; otherwise the winning rule's zone name, which is "unserved" if that zone is -; or "none" if no rule matches.
Example 1
- Input:
- [["region:10***","city:10200-10299","depot:10250"],["10250","10299","10300","11000","10250-1234","1025","ab123"]]
- Output:
- ["depot","city","region","none","depot","invalid","invalid"]
- Explanation
10250matchesregion(1000 codes),city(100 codes) anddepot(1 code); the fewest codes wins, sodepot.10299matches region and city, socity;10300onlyregion;11000nothing.10250-1234is a valid ZIP+4 and uses10250.1025is too short andab123is not numeric, so both are invalid.
Example 2
- Input:
- [["east:20000-29999","-:21000-21499","west:21200-21299","north:21400-21499","later:21400-21499"],["20500","21100","21250","21450","29999","30000"]]
- Output:
- ["east","unserved","west","north","east","none"]
- Explanation
21100matcheseast(10,000 codes) and the-rule (500 codes), and the narrower one wins, so the code isunserved.21250also matcheswest(100 codes), which is narrower still.21450is covered by the-rule and bynorthandlater, both with 100 codes; they tie, andnorthis listed first.
Example 3
- Input:
- [["!hub:90000-90099","town:90050","cheap:9****","!wide:5****","!mid:50***","c:50100","low:00500-00599"],["90050","90100","50100","51000","60000","00501"]]
- Output:
- ["hub","cheap","mid","wide","none","low"]
- Explanation
90050matches the overridehuband the narrowertown, but the override wins.90100matches onlycheap. Overrides compete among themselves by size:50100is inside!wide(10,000 codes),!mid(1,000 codes) and plainc;midwins.51000is only in!wide.60000matches nothing, and00501is the number 501, whichlowcovers.
Constraints
0 <= rules.length <= 200and0 <= queries.length <= 500- Zone names are 1 to 12 lower-case letters or digits, or exactly
-; in a rangelo <= hi; in a wildcard the digits come first and the*characters last - Queries are arbitrary strings of up to 12 printable ASCII characters