UST global Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
public class OneCount {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print(count(67, true));
}
private static int count(int input, boolean isFirstCall) {
int tmp = 1;
if(isFirstCall)
{
tmp = 2;
}
if (input == 1)
return 0;
if (input % 2 == 1 && ((input / 2) % 2) == 1)
return tmp + count(input / 2,false);
else
return count(input / 2,false);
}
}
void findMax(int num, int& len)
{
static int tempLen = 0;
static bool found = true;
//cout<<" num = "<<num<<endl;
int k = 0;
if(!num)
return;
else
{
k = num & 1;
if(k == 0)
{
if(len < tempLen)
len = tempLen;
tempLen = 0;
found = false;
}
else
{
if(!found)
found = true;
tempLen++;
}
}
findMax(num>>1, len);
}
int calNoBitSet ()
{
unsigned int n,c = 0,tmp,i;
unsigned int BitsSetTable256[256];
printf ("Enter Number\n");fflush(0);
scanf ("%d",&n);
//Bryan kerigham way , method runs for number of times equal to the set bits in the number
tmp = n;
for (c = 0; tmp; c++)
tmp &= (tmp - 1);
printf ("[Kerigham] Bit Count int %d => %d\n",n,c);
//Naive method runs 32 times in worst case if the set bit is the msb
tmp = n;
for (c = 0; tmp ; tmp = tmp>>1)
c = c + (tmp & 1);
printf ("[Naive] Bit Count int %d => %d\n",n,c);
//Look up table method
// To initially generate the table algorithmically:
BitsSetTable256[0] = 0;
for (i = 0; i < 256; i++)
{
BitsSetTable256[i] = (i & 1) + BitsSetTable256[i / 2];
// printf ("Bit set in table %d => %d / %d\n",i,BitsSetTable256[i], BitsSetTable256[i / 2]);
}
//lookup m1
c = BitsSetTable256[n & 0xff] +
BitsSetTable256[ (n>>8) & 0xff] +
BitsSetTable256[(n>>16) & 0xff] + BitsSetTable256[(n>>24)];
printf ("[Look Up - M1] Bit Count int %d => %d\n",n,c);
//lookup m2
c = BitsSetTable256[*((unsigned char *)&n)] +
BitsSetTable256[*(((unsigned char *)&n)+1)] +
BitsSetTable256[*(((unsigned char *)&n)+2)] + BitsSetTable256[*(((unsigned char *)&n)+3)];
printf ("[Look Up - M2] Bit Count int %d => %d\n",n,c);
return c;
}
#include<stdio.h>
int main()
{
int a,bin,len=0,maxlen=0,temp,prev;
printf("enter the number");
scanf("%d",&a);
temp=a;
bin=temp%2;
temp=temp/2;
if(bin==1)
len++;
prev=bin;
while(temp!=0)
{
bin=temp%2;
temp=temp/2;
if(bin==1&&prev==0)
len=1;
else if(bin==1&&prev==1)
{
len++;
}
else
{
if(len>maxlen)
{
maxlen=len;
len=0;
}
}
prev=bin;
}
if(len>maxlen)
{
maxlen=len;
}
printf("%d",maxlen);
return 0;
}
The question is to find maximum number of consecutive 1's.
here first find the rightmost one, start counting number of 1's from the rightmost 1, keep a count of it. then once you encounter 0, set those 1's to 0, then countinue right.
for example: 0b11110011 then max 1 is 4.
# include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
int x= 0xff3f;
int rightmost1,count, max,i,j;
max =0;
while(x)
{
rightmost1=(x&-x);
if(rightmost1)
{ count = 0;
while(x &rightmost1)
{count++;
rightmost1 = rightmost1 << 1;
}
}
for(i=0;i<count;i++) x= (x & (x-1));
if(count > max ) max = count;
}
printf(" max = %d",max);
}
The question is to find maximum number of consecutive 1's.
here first find the rightmost one, start counting number of 1's from the rightmost 1, keep a count of it. then once you encounter 0, set those 1's to 0, then countinue right.
for example: 0b11110011 then max 1 is 4.
# include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
int x= 0xff3f;
int rightmost1,count, max,i,j;
max =0;
while(x)
{
rightmost1=(x&-x);
if(rightmost1)
{ count = 0;
while(x &rightmost1)
{count++;
rightmost1 = rightmost1 << 1;
}
}
for(i=0;i<count;i++) x= (x & (x-1));
if(count > max ) max = count;
}
printf(" max = %d",max);
}
#include<stdio.h>
int main(int argc,char* argv[])
{
if(argc < 2)
printf(" pass integer value to be found");
int loop=0;
int count = 0;
int max1s = 0;
int val = atoi(argv[1]);
for(loop = 0; loop < sizeof(int)*8; loop++)
{
if((val&(1<<loop)) != 0 )
count++;
else
count=0;
if(max1s < count)
{
max1s = count;
}
}
printf("Max 1 s in a number %d is %d\n", val, max1s);
return 0;
}
public class HelloWorld{
public static int numberOfConsecutiveOnes(int number){
int temp = 0;
int count = 0;
for(int i = 0;i < 32;i++){
if((number & (1 << i)) != 0) temp++;
else temp = 0;
count = Math.max(count, temp);
}
return count;
}
public static void main(String []args){
System.out.println(numberOfConsecutiveOnes(206));
}
}
public class HelloWorld{
public static int numberOfConsecutiveOnes(int number){
int temp = 0;
int count = 0;
for(int i = 0;i < 32;i++){
if((number & (1 << i)) != 0) temp++;
else temp = 0;
count = Math.max(count, temp);
}
return count;
}
public static void main(String []args){
System.out.println(numberOfConsecutiveOnes(206));
}
}
public class HelloWorld{
public static int numberOfConsecutiveOnes(int number){
int temp = 0;
int count = 0;
for(int i = 0;i < 32;i++){
if((number & (1 << i)) != 0) temp++;
else temp = 0;
count = Math.max(count, temp);
}
return count;
}
public static void main(String []args){
System.out.println(numberOfConsecutiveOnes(206));
}
}
public class HelloWorld{
public static int numberOfConsecutiveOnes(int number){
int temp = 0;
int count = 0;
for(int i = 0;i < 32;i++){
if((number & (1 << i)) != 0) temp++;
else temp = 0;
count = Math.max(count, temp);
}
return count;
}
public static void main(String []args){
System.out.println(numberOfConsecutiveOnes(206));
}
}
in c
#include <stdio.h>
#include <string.h>
main()
{
printf("%d", numberOfConsecutiveOnes(127) );
}
int numberOfConsecutiveOnes(int number){
int temp = 0;
int count = 0;
int i;
for(i = 0;i < 32;i++){
if((number & (1 << i)) != 0) temp++;
else temp = 0;
if(temp > count) count = temp;
}
return count;
}
working code for any input no :-
please add comments
#include<stdio.h>
int maxOneBits(int no){
int max = 0, count = 0;
if(no == 0)
max = 0;
else{
while(no > 0){
if(no % 2 == 1){
count++;
}
else{
if(count > max)
max = count;
count = 0;
}
no = no / 2;
}
if(count > max)
max = count;
}
return max;
}
int main()
{
int no, result;
scanf("%d",&no);
result = maxOneBits(no);
printf("Max one bits = %d\n",result);
return 0;
}
#include<stdio.h>
- leela July 04, 2014int main()
{
int num = 67;
int samp=1;
int count =0,max=0;
int i=0;
for(i=0;i<32;i++)
{
while (num & samp<<i )
{
count++;
if(count >max)
max=count;
i++;
}
count=0;
}
printf("%d",max);
return 0;
}