Ooyala Interview Question: Given an array, shift all the... | Glassdoor

Interview Question

Software Engineer Interview Santa Clara, CA

Given an array, shift all the zeros to the right of the

  array, without disrupting order of the non zero elements

Interview Answer

2 Answers


Using O(N) space we can do this using a hashmap. Key is the number and value is the index in the array. Then go through and place all the non zero elements before the zeros. O(n) time. I think there is a solution to this that does not use any extra space as well.

Anonymous on Oct 8, 2015

zeroShift = (arr)=> arr.filter((num)=>num!=0).concat(arr.filter((num)=>num===0))

Anonymous on Feb 20, 2017

Add Answers or Comments

To comment on this, Sign In or Sign Up.