Solving Multisoft's Monthly Code Challenge

Solving Multisoft's Monthly Code Challenge

Written by Kim Hallberg on Jul 6th, 2021 Views Report Post

Swedish software company Multisoft does a code challenge each month, where it provides a fun little challenge for developers to solve. They do this in hopes of finding new candidates for potential employment.

In this post, we will go over and solve this month's following code challenge.

input = "0219247151", output = ""

for (i in 1 .. length(input))
    if (input[i]%2 == input[i-1]%2)
        output += max(input[i], input[i-1])

The challenge is written in pseudo-code and we want to know what will be stored in the output variable when the algorithm is finished.

The algorithm in itself is relatively simple and doesn't have very many working parts, but can be tricky if you're not paying close attention to the setup.

The setup for the algorithm creates two variables, we will call them input and output for simplicity. Our input stores our initial numbers, and output will store the result set of our for operation.

The Algorithm.

The algorithm will step over each number in the input string starting from the second index through to the last number, in each iteration we will check if the current and previous index have the same remainder when divided by 2.

When both indexes have the same remainder, we add the maximum value to our output variable.

The Solution.

Given the above algorithm, we could expect different results based on how a language handles string and integer concatenation.

If for instance, our language coerces the string returned from max to integers during the concatenation assignment, we could end up adding each result to each other instead of concatenating them.

+= usually indicated an arithmetic addition of the previous value added to our new value. In this instance though it indicated a string concatenation assignment.

Given that, stepping through the algorithm will look something like this.

Visual representation of the algorithm

Storing the final string value of 294755 in to the output variable.

In Conclusion.

This was a fun little challenge done by Multisoft, and I look forward to next month's challenge.

More challenges.

If you're interested is solving coding challenges, check out some of the links below.

Comments (0)