Order Layout
Updated
Problem statement
A barcode scanner is set up by scanning barcodes in the right order. All of its configurations are stored together in one string, layout. Your job is to check the string and list the configurations in order.
The string holds pieces separated by '|'. Each piece is a 4-digit index followed by a configuration value. For example, the piece "0001LAJ5KBX9H8" has the index 0001 and the value "LAJ5KBX9H8".
The string is valid only when all of these hold:
- Every piece is a 4-digit index followed by a value, and the pieces are separated by '|'.
- With
npieces, the indexes are exactly 1, 2, ...,n. No number is skipped. - No index appears twice. "0000" is not a valid index.
- Every value has exactly 10 characters, and each character is a letter or a digit.
- No value appears twice. Upper case and lower case letters count as different.
If the string is valid, return the values sorted by their index. Otherwise, return ["Invalid configuration"].
For example, let layout = "0002BBBBBBBBBB|0001AAAAAAAAAA". The indexes are 0002 and 0001, and both values have 10 letters. The answer is ["AAAAAAAAAA", "BBBBBBBBBB"].
Function
ordaLayout(layout: String) → String[]
Complete the function ordaLayout in the editor.
ordaLayout has the following parameter:
String layout: the encoded configuration string
Returns
String[]: the values in index order, or ["Invalid configuration"]
Examples
Example 1
layout = "0001LAJ5KBX9H8|0003UKURNK403F|0002MO6K1Z9WFA|0004OWRXZFMS2C"return = ["LAJ5KBX9H8", "MO6K1Z9WFA", "UKURNK403F", "OWRXZFMS2C"]Every piece has a 4-digit index and a 10-character value. The indexes 0001 to 0004 each appear once, and the values are all different. Sorted by index, the values are "LAJ5KBX9H8" (0001), "MO6K1Z9WFA" (0002), "UKURNK403F" (0003) and "OWRXZFMS2C" (0004). The answer lists them in that order.
Example 2
layout = "000533B8XLD2EZ|0001DJ2M2JBZZR|0002Y9YK0A7MYO|0004IKDJCAPG5Q|0003IBHMH59SBO"return = ["DJ2M2JBZZR", "Y9YK0A7MYO", "IBHMH59SBO", "IKDJCAPG5Q", "33B8XLD2EZ"]The indexes are 0005, 0001, 0002, 0004 and 0003, so 0001 to 0005 each appear once. All values have 10 characters and are different. Sorted by index, the values are "DJ2M2JBZZR", "Y9YK0A7MYO", "IBHMH59SBO", "IKDJCAPG5Q" and "33B8XLD2EZ". The answer lists them in that order.
Example 3
layout = "0002f7c22e7904|000176a3a4d214|000305d29f4a4b"return = ["76a3a4d214", "f7c22e7904", "05d29f4a4b"]The indexes are 0002, 0001 and 0003. Lower case letters are fine in values. Sorted by index, the values are "76a3a4d214", "f7c22e7904" and "05d29f4a4b". The answer lists them in that order.
Example 4
layout = "0002f7c22e7904|000176a3a4d214|000205d29f4a4b"return = ["Invalid configuration"]Index 0002 appears twice, and index 0003 is missing. So the string is not valid. The answer is ["Invalid configuration"].
Constraints
1 <= layout.length <= 150000layouthas 1 to 9999 pieces separated by'|'layouthas only English letters, digits and the characters'|','-','_','.'and'#'- Indexes and values in
layoutmay repeat or be missing
More Amazon interview questions
- Add Course Batches All or NothingONSITE INTERVIEW · Seen Dec 2026
- Priority Ad Rotation with Show LimitsONSITE INTERVIEW · Seen Dec 2026
- Next Greater Element IONSITE INTERVIEW · Seen Dec 2026
- Count Connected ComponentsPHONE SCREEN · Seen Dec 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Merge Accounts by Shared AttributePHONE SCREEN · Seen Dec 2026
- Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Minimum Grid InconvenienceOA · Seen Dec 2026
- Optimal Account BalancingONSITE INTERVIEW · Seen Dec 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
Similar questions at other companies
- Remove CSS Comments and Find Hex ColorsAdobe · OAAdobe interview questions
- Rank Word-Translation Hints from Sentence TranslationsDuolingo · PHONE SCREEN · ONSITE INTERVIEWDuolingo interview questions
- Response Time Stats by URL PatternGusto · ONSITE INTERVIEWGusto interview questions
- Top K Movies by Play Count from CSV DataTubi · PHONE SCREEN
- Transform Dotted Keys into a Nested MapProofpoint · ONSITE INTERVIEW
- Serialize and Deserialize a String Key-Value StoreByteDance · ONSITE INTERVIEWByteDance interview questions
- Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
- Parse and Order Structured LogsCoinbase · ONSITE INTERVIEWCoinbase interview questions
- Deep Merge with a Conflict ResolverAutodesk · ONSITE INTERVIEWAutodesk interview questions
- Common Words Across FilesMeta · PHONE SCREENMeta interview questions