title: Programming interview questions I had: Reverse a string and find the mode of an array
body: <p>I had a C++ programming test for a big tech company which shall not be named. I was given the following chunks of code and asked to use them to reverse a string and return the mode of an array. </p>
  
  <pre><code>char * reverse(char * str, int n)
  {
     ...
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>int mode(int * arr, int n)
  {
     ...
  }
  </code></pre>
  
  <p>I'm not sure if I did them the "best" way, but I'm curious how the high priests of Stack Overflow would do them. I'm also curious why they gave me such a strange prototype in the first case. Wouldn't it be better as a void function? And shouldn't they have given my <code>size_t</code> for <code>n</code> in both cases?</p>
  
  <p>I wrote</p>
  
  <pre><code>for (char* i(str), j(str+n); i &lt; j; ++i, --j)
      std::swap(*i, *j)
  return str;
  </code></pre>
  
  <p>and</p>
  
  <pre><code>if (n == 0)
      throw("Can't have the mode of an empty array, bro.")
  std::map&lt;int,int&gt; cntMap;
  std::pair&lt;int,int&gt; top(arr[0], 1);
  cntMap.insert(top);
  for (int k(1); k &lt; n; ++k)
  {
      if (cntMap.count(*k) == 0)
         cntMap.insert(*k, 1);
      else
         ++cntMap[*k];
      if (cntMap[*k] &gt; top.second)
         top = std::make_pair(*k, cntMap[*k])
       return top.first;       
  }
  </code></pre>
  
  <p>Did I fail?</p>