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.
you don't need to do that tho
There is no point in checking the return of
malloc()
on Codewars, because you are given no instructions on what to do if it fails. In large C projects, error handling is usually centralized andmalloc()
and other system calls are usually wrapped in a function or macro that handles the potential failures. But what to do in case of failure is specific to each project and to each system call: sometimes you want to exit the program immediately, sometimes you want to try and recover the error, sometimes you want to ask the user how to proceed.On Codewars, your code run in a Docker container and the docker image will be shut down when the tests are over, regardless of whether they crashed or not. So whatever error handling the user performs in their code is ultimately irrelevant.
On another note: on some systems and with a certain configuration,
malloc()
does not necessarily returnNULL
when it does not find enough memory to give you; instead, the operating system will pretend like it did, in the hope that either you won't access all of it, or that it will manage to free some memory from other processes to give it to yours by the time you actually access it:https://stackoverflow.com/questions/16674370/why-does-malloc-or-new-never-return-null
Often the description says something like "You can expect the input to be valid".
Since it is missing in this case the top solution should indeed include sanity checks/error handling imo.
i agree, although this solution may appear as clever it doesn't resemble the best practices.
Thank you for the explanations. I think that I am getting a bit closer to understand all of this.
Trying to understand the complexity here, am I right to think this is quadriatic due to fact that each slice operation time complexity is (omega) O(k). So as we doing two of them it is O(k) * 2 = O(k^2). Then there is iteration cost which is O(n). Not sure if this can be seen as k elements in the list as well therefore with slicing together ending in worst case scenario complexity of O(k^2) * k or O(k^3). Does this make any sense? :-)
Souce of info: https://wiki.python.org/moin/TimeComplexity