I’ve found this interview question at GeeksForGeeks and decided to actually code it in C++. The program currently prints the “value” of the horses (which is inaccessible by the coder so they can’t just look up who is number 1) and then proceeds to sort them and find the position of numbers 1,2 and 3 in a 5x5 matrix with the solution being a minimum of 7 races.

Here’s the Github repo: https://github.com/Shroomerian/HorseRacing

  • ShroomerOP
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 year ago

    After a few hours of work, I was able to improve the code by following your recommendations. I was genuinely surprised at how removing boilerplate code that I created at the beginning helped me see where the problems are and allowed me to see better solutions to problems.

    For example: instead of copying each value into a temporary int array, sorting that array and then changing the original, I was able to just use C++ STL sort() with a function parameter to automatically sort structs based on their values when it came to the rows.

    I touched on every point that you’ve made and made changes accordingly, some things like the char option I just removed and checked for the conditions in the function instead of waiting for the programmer to give the correct answer. The only thing left for me is to relax a bit since I was at it for a lot of time, and then properly comment the code.