FlexTrade Interview Question for Software Engineer / Developers






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

read first element of array to determine whether its 1 or zero.
start binary search depending on above result in order to find end of 1 or zero

- Anonymous March 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why do you have to make things complicated? You going to write a bsearch for this?
You can just run through it once, know that it's N 1s (or 0s), then minus it by the total array elements since it's an array.. or vec.size(), if they need to know the remainder.

- Anonymous June 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the reason binary search is SPEED.
BSearch will get answer in log(n) searches.
Looping through will require n/2 operations in avrg case. And worst case is n operations

- jetjeet August 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

+1

O(log n) is better than O(n)

- a626 October 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Binary search wont guarantee the first instance of the value .best would be count one occurrence and subtract it from the length

- Arun February 16, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How can you be sure that the binary search will return the index of the last 1? It could just as easily find an element that's 1 in the middle of the group of 1's.

- New Yorker May 24, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Quote - " either 1 or 0 , and they are in sorted order Ex. a [] = { 1,1,1,1,0,0,0}"

Sorted order ..so shouldn't it be a[] = { 0,0,0,1,1,1,1} instead of 1s first. hello?
They need to phrase it correctly. sigh what's the happen with these interviewers..or is sachinsaner interpret or wrote it wrong.

- Anonymous May 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes! Why people are complicating this?:P

- Avenger July 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

Get the sum of the array using a for loop. The sum of array will be the number of 1's and arrayCount - sum of the array will be the number of 0's.

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

I believe that this solution will be correct:

public class Test_CheckNumberOfDigitsInArray
{

public static void main(String[] arghs)
{
int a [] = { 1,1,1,1,0,0,0};

int noOfOnes = 0;
int noOfZeros = 0;

for (int i=0; i<a.length; i++)
{
switch (a[i])
{
case 1:
noOfOnes += 1;
break;
case 0:
noOfZeros += 1;
break;
}
}

System.out.println("Number of 1's is " + noOfOnes
+ " and number of 0's is " + noOfZeros);

}
}

- newby March 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The question pertains to C++, so you can't get the length of the array as a property like in Java.

- New Yorker May 24, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main()
{
int a[]={1,1,1,1,0,0,0};
int size=sizeof(a)/sizeof(a[0];
int sum =0;
for(int i=0;i<size;i++)
sum+=a[i];
cout<<"no of 1s"<<sum<<endl<<"no of 0s"<<size-sum<<endl;
return 0;
}

- Swam Master April 03, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>

using namespace std;

int binary_search(int* arr,int low,int high){
  if(high<low) return -1;
  int mid = (low + high)/2;

  if(arr[mid]==0 && (mid-1<0 || arr[mid-1]==1)){ // First element
      return mid;
  }
    
  else if(arr[mid]==0){  // Left search
    return binary_search(arr,low,mid-1);
  }

  else{  // Right search
    return binary_search(arr,mid+1,high);
  }
  
}

int main(){

  /* I am assuming that there are no integers apart from 0 and 1 */
  
  int arr[]={1,1,1,1,0,0,0}; // Given case
  /*
  int arr[]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0}; // More of the same
  int arr[]={0,0,0,0,0,0}; // Edge case
  int arr[]={1,1,1,1,1,1}; // Edge case
  int arr[]={}; // Edge case
  */

  int len = sizeof(arr)/sizeof(int);
  auto elem = binary_search(arr,0,len-1);
  
  if(elem==-1 && len!=0){ // Edge case where arr has all ones
    cout<<"Number of one's is "<<len<<" and number of zeros is 0"<<endl;
  }

  else if(elem==-1){
    cout<<"There are no elements present"<<endl;
  }

  else{
    cout<<"Number of one's = "<<elem<<" and number of zero's = "<<len-elem<<endl;
  }
  return 0;
}

- Anonymous May 28, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please delete the semicolon on line no. 9.

- Ashish Tiwari May 28, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please delete the semicolon on line no. 9

- Anonymous May 28, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Please cancel out that semicolon on line 9. I made a mistake while pasting it.

- Ashish Tiwari May 28, 2019 | Flag Reply


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