Interview Question






Comment hidden because of low score. Click to expand.
0
of 0 vote

Count the number of zeros..fill the array with count of zeros and remaining with 1's..

- Musheka January 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Above approach does involve two traversals of the array.
Alternative:
Use two pointers at the two ends of the array. Increment left pointer till you encounter a 1, Decrement right pointer till you encounter a 0 (just like Quick Sort).
If left pointer < right pointer, swap the elements.

- Anonymous January 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

here it is O(n/2) = O(n)

def segBinary():
  e = [0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0]
  
  j = len(e)-1
  for i in range(0, len(e)/2):
    if e[i] == 1:
      while e[j] != 0:
        j-=1
      one = e[i]
      e[i] = e[j]
      e[j] = one
  print e

- binary bill February 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The loop condition is wrong, and would produce and invalid answer for e=[0,0,0,1,0]. Instead of a for, how about while i < j?

- Anonymous January 03, 2011 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More