Question from an interview yesterday - one of the top eCommerce company

You are given an input array with only 1's and 0's something like this [1,0,0,0,1]. Write code to optimally place 1's in place of a '0' so there are maximum number of 1s in the array. The placement of 1 should not be next to another '1'.

Answer to the above array is [1,0,1,0,1]. Placed only one 1

This one sounds simple, scan every element and place a 1 if there are no 1s next to it. Am i missing some scenario where this would simple logic fails?

Comments (2)