Finished
33// convenience to get you started writing code faster.
44//
55
6export const encode = () => {
7 throw new Error('Remove this line and implement the function');
6export 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
854};
955
10export const decode = () => {
11 throw new Error('Remove this line and implement the function');
56export 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
12100};
Test NameStatus
incomplete sequence causes error
Fail
incomplete sequence causes error, even if value is zero
Fail
zero
Pass
arbitrary single byte
Pass
asymmetric single byte
Pass
largest single byte
Pass
smallest double byte
Pass
arbitrary double byte
Pass
asymmetric double byte
Pass
largest double byte
Pass
smallest triple byte
Pass
arbitrary triple byte
Pass
asymmetric triple byte
Pass
largest triple byte
Pass
smallest quadruple byte
Pass
arbitrary quadruple byte
Pass
asymmetric quadruple byte
Pass
largest quadruple byte
Pass
smallest quintuple byte
Pass
arbitrary quintuple byte
Pass
asymmetric quintuple byte
Pass
maximum 32-bit integer input
Pass
two single-byte values
Pass
two multi-byte values
Pass
many multi-byte values
Pass
one byte
Pass
two bytes
Pass
three bytes
Pass
four bytes
Pass
maximum 32-bit integer
Pass
multiple values
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.