The basics of programming in c++ for beginners

A task: “simplify code”

There is a whole class of problems, consisting in the wrong, what “write your code to…”, and so: “in front of you ready code – simplify and shorten its”. A large Israeli company sent me this kind of problem as a test for a vacancy. This is a very interesting class of problems, which the:

    • learn to understand other people's code;
  • requires fine knowledge of syntax to simplify.

For a practical programmer, such tasks are more valuable., than just to write … Hello World! Interesting then, that the task code should be simplified almost without understanding what this code should do – according to the rules of the formal syntax of C ++.

In this problem,:
func1() – exactly the source code function, which should be simplified;
func2() – that is what I got after simplification;
– … and everything else (main) – this is a test of comparing the performance of 2 options on several examples.


Execution result:

c ++ problem and solution, source, programming practice to c ++

5 thoughts on “A task: “simplify code”

  1. Well, the result of the test then needs to be shown for completeness.:

    $ ./strsimpl
    1 : => 3[2] ... 3[2]
    2 : => 7[6] ... 7[6]
    3 : => 3[2] ... 3[2]
    4 : => 5[4] ... 5[4]
    5 : => 6[5] ... 6[5]
    6 : => NULL ... NULL
    7 : => NULL ... NULL
    8 : => "" ... ""

  2. By the way…
    Our Israeli friends think this decision is wrong – they dreamed of something more kosher ;-)

    Although, on the other hand, I consider, that they just don't know how to accurately articulate their kosher dreams.
    Here is literally their problem statement:

    Simplify the implementation below as much as you can.
    Even better if you can also improve performance as part of the simplification!
    FYI: This code is over 35 lines and over 300 tokens, but it can be written in 5 lines and in less than 60 tokens.

    Testing shows complete equivalence of func1() и func2().

    Nevertheless, this episode suggests, that the problem can have several interesting solutions. I suggest those who wish to actively engage in their search!

  3. The challenge is to optimize the code, your solution is the usual rewriting of the proposed algorithm in order to reduce the number of lines.

  4. ” the task code should be simplified almost without understanding what this code should do - according to the rules of the formal syntax of C ++.”
    But this does not really match the condition in the task itself. – there seems to be a need to simplify the implementation itself. Which means, you should first try to understand the meaning of the function, and then the best solution becomes obvious:

    1. Corrected a little. In your option, condition is not checked, when one of the parameters a and b is ‘ 0′, and the second one is not found in s, and the parameter s != “\0”.

      short i=0;
      do {
      if (s[i]==a || s[i]==b) return s+i;
      } while (s[i ]!=’’);
      return NULL;

Leave a Reply

Your email address will not be published. Required fields are marked *