The task is to add two binary numbers of various length. There is more than one possible solution. First, the basic solution which doesn’t use too many builtin Python functions works as follows: Reverse the strings containing the numbers. Then Iterate in a loop as long as we didn’t reach the end of both strings. Write the current position to a temporary variable if it’s still in the boundaries of the string. Then add a potential overflow and handle it if the sum of a + b + overflow > 1
. Finally, check for the overflow at the end and add a 1
and reverse the string again before returning it.
This can be solved much simpler with builtin Python functions.
A decimal number is given as a list of integers. The number should be incremented by 1
. The idea here is to iterate backwards through the list and start with the smallest digit. This way, this challenge can be solved in O(n)
. If after adding 1
, the mod(10)
is 0
it means we had an overflow and need to add one. If we didn’t we’re done and can break the loop. As a final check we need to make sure that there is no overflow in the end which would cause us to prepend the list.
There is a given number of stairs n
. Stairs can be either taken two steps at a time or one step at a time. For a height of 1
, there is only one possible solution: 1
. For a height of 2
, there are two solutions: 1-1
or 2
.
The task is to find out of a series of stock prices the best time to buy and sell the stock. Only one stock can be owned at the time and it can be sold and bought at the same time.
The task is to find out if a given string is comprised of multiple identical substrings. The first observation is that the substring cannot be longer than half of the length of the original string. The second observation is that the length of the total string has to be a multiple of the string that is tested, if it is not it can be discarded right away. Lastly, a string with the length of one can be discarded immediately.