Classic programming interview questions I'm going to post some of my solutions to common interview questions. I would love it if you could critique me from a C++ purist's standpoint. Also, tell me if there's a better algorithm for each one of these. 

Find the mode (most common element) of an array.

Note: My solution returns an arbitrary mode if there's a tie; assumes the array is non-empty, too

int mode(int* arr, int sz) { 
    std::map&lt;int,int&gt; cntMap;
    int most = arr[0]; 
    for (int i = 0; i &lt; sz; i++) {
       if (!cntMap.count(arr[i])) {
          cntMap[arr[i]] = 0;
       } else {
          cntMap[arr[i]]++;
       }
       if (cntMap[arr[i]] &gt; most) {
          most = cntMap[arr[i]];
       }
    }
    return most;
}


Sum the largest 2 largest elements of an array.

Note: Assumes the array contains at least 2 elements

int largest2(int* arr, int sz) { 
    int largest(arr[0]), secondLargest[arr[0]];
    for (int i = 0; i &lt; sz; i++) { 
       if (arr[i] &gt; largest) {
          secondLargest = largest; 
          largest = arr[i];

       } else if (arr[i] &gt; secondLargest &amp;&amp; arr[i] &lt;= largest) { 
          secondLargest = arr[i];
       }
    }
    return (largest + secondLargest);
}


Find whether an array as repeated elements, where it's known that the numbers of the array are between 1 and 100.

bool contains_repeats(int* arr, int sz) { 
    std::vector&lt;bool&gt; booVec(100,false);
    for (int i = 0; i &lt; sz; i++) { 
       if (booVec[arr[i]-1]) { 
           return true; 
       } else { 
           booVec[arr[i]-1] = true;
       }
    }
    return false;
}
Is the signature of the function imposed ? Your solution would work for any kind of iterable objects and would be probably more idiomatic than passing a pointer and a size. Also, the logic would work for any (comparable) types and not just int so it might be worth making it a templated function.
In your search of the most common element, do you want to return the number of occurences or the element itself ?
In your sum of the two biggest, I think you can compare to largest only once.


You just need to do something like :

   if (arr[i] &gt; secondLargest) {
       if (arr[i] &gt; largest) { 
           secondLargest = largest; 
           largest = arr[i];
       } else {
           secondLargest = arr[i]; 
       }
   }