## FlexTrade Interview Question for Software Engineer / Developers

• 0

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

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

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.

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

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

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

+1

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

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

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

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

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.

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.

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

yes! Why people are complicating this?:P

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.

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);

}
}

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

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

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;
}

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;
}``````

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

Please delete the semicolon on line no. 9.

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

Please delete the semicolon on line no. 9

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.

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.

### 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.