Why is everyone so fond of interviews and tests for recursion?

As my practice in life and real projects shows personally, the need for this is very low, they are used more often by foreach, but for all, with enviable persistence, everyone is trying to be assessed on it))

Closed due to the fact that it is necessary to reformulate the question so that it was possible to give an objectively correct answer by the participants of Grundy , pavel , aleksandr barakin , Kromster , Denis Nov 30 '16 at 6:42 .

The question gives rise to endless debates and discussions based not on knowledge, but on opinions. To get an answer, rephrase your question so that it can be given an unambiguously correct answer, or delete the question altogether. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    Because it is more difficult? On simple tasks it is not so easy to distinguish a smart applicant from a not very smart one. (Although you can, if you try.) - VladD
  • I also rarely used recursion in practice (although sometimes it is difficult to do without it), but if the applicant doesn’t understand the principle of recursion poorly, then in general the algorithms are most likely so-so. It is like a kind of indicator, how fast a person gets into algorithms. - A1essandro
  • @VladD and what is difficult in it? - pavel
  • @pavel: A philosophical question :) In my opinion, it is more complicated than a regular iteration, because the recursive approach implies a certain degree of abstract thinking. - VladD
  • one
    @pavel: This is because you had to work with a naturally recursive structure (tree). Usually, recursive data structures are not often encountered, so natural algorithms are mostly iterative. - VladD

5 answers 5

There are a number of tasks for which recursion is best suited. For example, bypassing tree structures. Of course, it is used much less frequently than regular cycles, but almost any programmer sooner or later encounters it. Its use at interviews can be explained by this - on the one hand, any self-respecting programmer should know what it is, and on the other hand, it is used not so often, therefore there is a possibility that a very weak candidate will not remember anything worthwhile an experienced person will be able to talk about the benefits of recursion from his own experience, and a person without much experience, but with a head on his shoulders and / or with a sufficient theoretical basis, can at least tell something from memory.

    I agree with the first paragraph of the answer Zealint, but strongly disagree neither with the second paragraph, nor, especially with the conclusions.

    As my personal practice shows in life and real projects, the need for this is very low, more often foreach

    Well, that's exactly what the employer wants to find out; understanding the fact that cycle and recursion are two completely different algorithms with different fields of use; and experience in solving problems for which recursion is required.

    Although I personally would not ask to write a recursion code at the interview, but I would give two tasks to bypass the tree, one of which is solved by a cycle and the second by recursion, and I would just ask how the applicant would solve each. Just to cut off those familiar with recursion by hearsay, who would use it to find a parent.

      In my opinion, there are two reasons for the popularity of such issues.

      1. Check abstract thinking. Agree that you need to keep in mind some imaginary model in which the function calls itself (especially interesting when 2-3 functions call each other mutually), while the program does not loop, works correctly and does not destroy the stack. To do this is always right, you must at least intuitively own the apparatus of the method of induction from mathematics, and this is due to the ability of man in principle to think logically. Of course, recursion is not the only good thing to test this ability.

      2. Low level of culture of employers (or those who conduct an interview). As a rule, typical tasks for recursion (to alter a cycle into recursion, to calculate the sum of a series through recursion), which are fully defined here on SO, are absolutely useless, because everything is solved more correctly in a cycle. You are either just being checked whether you will perform the task without question, even if it is KILLED and DULL (they do not need to have their own opinion), or they have little idea why they really need recursion ... ask them themselves, to write the algorithm Karatsuba without recursion. That's what I understand - the problem.

      My advice, do not humiliate yourself, if you are given such tasks - turn around and leave, this is not your place. Unless of course, it does not matter to you exactly this place of work If you go along with stupid questions and readily answer them, you will work with them on stupid tasks all the time.

      IMHO.

      • 3
        I think the advice is too categorical, there are many tasks where recursion significantly reduces the number of lines in the code. Of course, we understand that any algorithm can be written without recursion, but why? - pavel
      • one
        You're right. But what you are talking about, I meant in paragraph 1. It is better to write the same algorithm of Karatsuba or Tooma-Cook only with recursion. The number of lines can be reduced at times. I expressed my opinion from the position of my view on the modern IT-sphere. In the field of algorithm implementation, such questions do not arise at all. - Zealint

      Because some tasks are much faster and easier to express recursively. And often such solutions are good because of their greater maintainability. There are not many passages everywhere, so the ability to write a recursive algorithm is very valuable.

      • 2
        And it is possible for those who vote against to prove what I am wrong? Maybe something new will find out. - Vadim Ovchinnikov

      Because the employer does not want to see in the code of his product a ladder from foreach. If the candidate does not know about recursion, then asking more complex questions does not make sense.