Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you're done. You have two lovely recursive functions, and a one-line call to them.</p> <p>I can only think of a hackish way to even do it in one function:</p> <pre><code>void mystrcat(char* to, const char* from, bool copy_now) { if (!copy_now &amp;&amp; *to) { mystrcat(to+1, from, false); return; } *to = *from; if (*to) mystrcat(to+1, from+1, true); } </code></pre> <p>That almost doesn't suck if you are using C++ and you make <code>copy_now</code> an optional parameter with a default value of <code>false</code>. Then the caller can pretend it doesn't have that extra bit of state.</p> <p>But if the problem is hard enough to make you ask for help on StackOverflow, it's too hard to be a reasonable interview question. You could ask it just to try to make the person think out of the box, but don't make failure to get a good answer an automatic flunk.</p> <p>I suggest adding another lovely recursive problem: find the depth of a binary tree.</p> <p>Given the following struct:</p> <pre><code>typedef struct s_node { struct s_node *pLeft; struct s_node *pRight; } NODE; </code></pre> <p>write a function</p> <pre><code>int tree_depth(NODE *pNode) </code></pre> <p>that returns the maximum depth of the tree. I was actually asked that question in an interview once and I think it is just the right level of difficulty for an interview. The people you don't want to hire will have trouble with it, and the people you might want to hire will probably enjoy it.</p> <p>EDIT: I tested the function and found it had a bug. This version has the bug fixed.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload