Problem · Array
Out-of-Order TLV File Reassembly
Learn this problemProblem statement
You receive one complete finite batch of unsigned 32-bit TLV packets in packets. Each value is stored in a long and uses this layout:
- Bits
31..28: message ID. - Bits
27..24: packet index. - Bits
23..20: packet count minus one. - Bits
19..18: field type, where0is a filename fragment,1is permission, and2is file data. - Bits
17..16: payload length. - Bits
15..0: payload.
Packets may appear in any input order. Within each message, process packets by ascending packet index.
- A filename or data packet has payload length
0,1, or2. For length1, the low byte is the fragment. For length2, read the high byte and then the low byte. Length0contributes an empty fragment. - A permission packet has payload length
1; its permission value is the low nine payload bits. - Concatenate filename fragments and data fragments independently in packet-index order.
Return one string per message in ascending message-ID order. Format each result as filename|permission|content, where permission is written in decimal.
Function
reassembleTlvFiles(packets: long[]) → String[]Examples
Example 1
packets = [591023947,556859491,540172590,573899172]return = ["a.c|420|OK"]All four packets have message ID 2 but arrive in index order 3, 1, 0, 2. Reordering them yields filename fragments "a." and "c", permission 420, and data "OK".
Example 2
packets = [304676929,54001664,270598264,19988579,287637997,3301934,37028224]return = ["b.c|384|","x|493|A"]The batch interleaves messages 0 and 1. The result is sorted by message ID. Message 0 has an empty data fragment, while message 1 reconstructs content "A".
Constraints
1 <= packets.length <= 256.- Each value is in the unsigned 32-bit range
[0, 2^32 - 1]. - Message IDs and packet indexes are in
[0, 15]. - Every message has one consistent packet count in
[1, 16], and exactly the packet indexes from0throughcount - 1appear once. - Field type
3does not appear. - Each message has at least one filename packet and exactly one permission packet.
- Filename and data payload bytes are printable ASCII other than
|. Unused payload bits are zero. - Permission is in
[0, 511].