| 3 | 3 | // convenience to get you started writing code faster. |
| 4 | 4 | // |
| 5 | 5 | |
| 6 | | export const encode = () => { |
| 7 | | throw new Error('Remove this line and implement the function'); |
| 6 | export const encode = (numbers) => { |
| 7 | // Edge Case: Empty input array |
| 8 | if (!numbers || numbers.length === 0) { |
| 9 | return []; |
| 10 | } |
| 11 | |
| 12 | const result = []; |
| 13 | |
| 14 | for (let i = 0; i < numbers.length; i++) { |
| 15 | const num = numbers[i]; |
| 16 | |
| 17 | // Edge Case: Negative numbers |
| 18 | if (num < 0) { |
| 19 | throw new Error('Number must be non-negative'); |
| 20 | } |
| 21 | |
| 22 | // Edge Case: Numbers larger than 32-bit unsigned integer |
| 23 | if (num > 0xFFFFFFFF) { |
| 24 | throw new Error('Number must be a 32-bit unsigned integer'); |
| 25 | } |
| 26 | |
| 27 | if (num === 0) { |
| 28 | result.push(0); |
| 29 | continue; |
| 30 | } |
| 31 | |
| 32 | const bytes = []; |
| 33 | let n = num; |
| 34 | |
| 35 | // Extract 7-bit chunks from right to left |
| 36 | while (n > 0) { |
| 37 | bytes.push(n & 0x7F); |
| 38 | n = Math.floor(n / 128); |
| 39 | } |
| 40 | |
| 41 | // Set continuation bit for all but the last byte (which is the first in our array) |
| 42 | for (let j = bytes.length - 1; j > 0; j--) { |
| 43 | bytes[j] |= 0x80; |
| 44 | } |
| 45 | |
| 46 | // Add bytes in reverse order (left to right) |
| 47 | for (let j = bytes.length - 1; j >= 0; j--) { |
| 48 | result.push(bytes[j]); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | return result; |
| 53 | // Handled Edge Cases: Empty input array, negative numbers, numbers larger than 32-bit unsigned integer |
| 8 | 54 | }; |
| 9 | 55 | |
| 10 | | export const decode = () => { |
| 11 | | throw new Error('Remove this line and implement the function'); |
| 56 | export const decode = (bytes) => { |
| 57 | // Edge Case: Empty input array |
| 58 | if (!bytes || bytes.length === 0) { |
| 59 | return []; |
| 60 | } |
| 61 | |
| 62 | const result = []; |
| 63 | let num = 0; |
| 64 | let hasStartedSequence = false; |
| 65 | |
| 66 | for (let i = 0; i < bytes.length; i++) { |
| 67 | const byte = bytes[i]; |
| 68 | |
| 69 | // Edge Case: Invalid byte values (greater than 255) |
| 70 | if (byte < 0 || byte > 255) { |
| 71 | throw new Error('Invalid byte value'); |
| 72 | } |
| 73 | |
| 74 | hasStartedSequence = true; |
| 75 | |
| 76 | // Add 7 bits to our number |
| 77 | num = (num << 7) | (byte & 0x7F); |
| 78 | |
| 79 | // If this is the last byte in the sequence (MSB is 0) |
| 80 | if ((byte & 0x80) === 0) { |
| 81 | // Edge Case: Overflow beyond 32-bit unsigned integer |
| 82 | if (num > 0xFFFFFFFF) { |
| 83 | throw new Error('Overflow error'); |
| 84 | } |
| 85 | |
| 86 | // Ensure we return as unsigned integer |
| 87 | result.push(num >>> 0); |
| 88 | num = 0; |
| 89 | hasStartedSequence = false; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // Edge Case: Incomplete byte sequence (last byte has continuation bit set) |
| 94 | if (hasStartedSequence) { |
| 95 | throw new Error('Incomplete byte sequence'); |
| 96 | } |
| 97 | |
| 98 | return result; |
| 99 | // Handled Edge Cases: Empty input array, invalid byte values, overflow beyond 32-bit unsigned integer, incomplete byte sequence |
| 12 | 100 | }; |