Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
closing as a user error and not an issue within the tests
You are mutating the
array
array because of a buffer overflow. Note thatsizeof
do not return the number of elements in an array but the size in bytes of its operand. So an array of 4int
s will have a size of 16 bytes because eachint
has a size of 4 bytes.Just fix this minor issue and your code pass :)
Random test cases in C have multiple integers with odd count but description says test cases shouldn't
examples:
for array = {7, 1, 7, 75, 75, 1, -11, -11, 7, 7, 75, -11, -11, -11, -11, 7, 7, 7, 1, 1, 75, 7, -11, 75, 75, 1, 75, 75, -11, 75, 1}
expected 1 but got 75
for array = {59, -34, 59, 59, 59, 59, 59, 59, 59, 59, 59, -34, -34, 59, 59, -34, -34, 59, 59, 59, -34, -34, 59, 59, 59, 59, 59, -36, 59}
expected -36 but got 59
The problem is in your code. In a
for
loop, you are assigning characters tosubarray
by index usingstrlen(subarray)
as your index. Essentially, assign the character to the last index of the subarray, but that doesn't quite work.strlen
works by finding a null terminator to find the end of the string, but you aren't null terminating on every iteration of the loop, so sometimesstrlen(subarray)
will return the incorrect length. I printed out the length ofsubarray
on every iteration of the loop. Here's a log from a test that failed in this way:Those
subarray
strlens are printed directly after the assignment of each character, and this log is only for the first word instr
"oyyeqqcu"
. So, it assignso
tosubarray
at index 0 first, and immediately afterwards,strlen
reports thatsubarray
has a length of 4, which is obviously not what you want.I just copied your current solution, removed the
free(subarray);
line and it worked.I've been trying to code a solution in C but on the random tests after it has ran a few times it begins inserting 3 random characters after the first letter of what would be the result. The test cases it fails on run fine in other compilers but I run into this bug everytime.
example:
for string:
"wrrwzf tky e nyjhrr qm cjitfy xxz tav"
expected:
"wrrwzf"
but got:
"w�4:rrwzf"