Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good C++ solutions to the "Bring all the zeros to the back of the array" interview challenge

Tags:

c++

algorithm

I had an interview for a Jr. development job and he asked me to write a procedure that takes an array of ints and shoves the zeroes to the back. Here are the constraints (which he didn't tell me at the beginning .... As often happens in programming interviews, I learned the constraints of the problem while I solved it lol):

  • Have to do it in-place; no creating temporary arrays, new arrays, etc.
  • Don't have to preserve the order of the nonzero numbers (I wish he would've told me this at the beginning)

Setup:

int arr[] = {0, -2, 4, 0, 19, 69}; 
/* Transform arr to {-2, 4, 19, 69, 0, 0} or {69, 4, -2, 19, 0, 0} 
   or anything that pushes all the nonzeros to the back and keeps
   all the nonzeros in front */

My answer:

bool f (int a, int b) {return a == 0;}
std::sort(arr, arr+sizeof(arr)/sizeof(int), f);

What are some other good answers?

like image 539
Failed Software Developer Avatar asked Nov 18 '14 20:11

Failed Software Developer


People also ask

How to prepare for programming job interviews in a short time?

If you need more such coding questions, you can take help from books like Cracking The Code Interview by Gayle Laakmann McDowell which contains 189+ Programming questions and solutions. A good book to prepare for programming job interviews in a short time. By the way, the more questions you solve in practice, the better your preparation will be.

How do I prepare for a DS Algo interview?

To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course. In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

How to answer interview questions about problem solving in a cover letter?

Whenever you answer interview questions about problem solving or share examples of problem solving in a cover letter, you want to be sure you’re sharing a positive outcome. Every employer wants to make more money, save money, and save time.


2 Answers

Maybe the interviewer was looking for this answer:

#include <algorithm>
//...
std::partition(std::begin(arr), std::end(arr), [](int n) { return n != 0; });

If the order needs to be preserved, then std::stable_partition should be used:

#include <algorithm>
//...
std::stable_partition(std::begin(arr), std::end(arr), [](int n) { return n != 0; });

For pre C++11:

#include <functional>
#include <algorithm>
//...
std::partition(arr, arr + sizeof(arr)/sizeof(int), 
               std::bind1st(std::not_equal_to<int>(), 0));

Live Example

Basically, if the situation is that you need to move items that satisfy a condition to "one side" of a container, then the partition algorithm functions should be high up on the list of solutions to choose (if not the solution to use).

like image 115
PaulMcKenzie Avatar answered Nov 08 '22 20:11

PaulMcKenzie


An approach that sorts is O(N*Log2N). There is a linear solution that goes like this:

  • Set up two pointers - readPtr and writePtr, initially pointing to the beginning of the array
  • Make a loop that walks readPtr up the array to the end. If *readPtr is not zero, copy to *writePtr, and advance both pointers; otherwise, advance only readPtr.
  • Once readPtr is at the end of the array, walk writePtr to the end of the array, while writing zeros to the remaining elements.
like image 22
Sergey Kalinichenko Avatar answered Nov 08 '22 22:11

Sergey Kalinichenko