UST global Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

#include<stdio.h>


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

- leela July 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

	}

}

- Venkat July 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

there is a problem with input of 6 which only return 1

- Anonymous October 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- instance July 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just read the number from right to left.
In Java:

int longestSequenceOfOnes(int input) {
  int max = 0;
  int current = 0;
  for (i = input; i > 0; i = i >> 1) {
    if (i % 2 == 1) {
      ++current;
    } else {
      max = Math.max(max, current);
      current = 0;
    }
  }
  return Math.max(max, current);
}

- djmclaugh July 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

unsigned int consecutive1s(unsigned int a)
{
	bool f=0;
	unsigned int cnt=0;
	unsigned int max=0;
	for(int i=0;i<32;i++)
	{
		if((a>>i)&1)
		{
			if(f==0)
			{
			f=1;
			}
			cnt++;
		}
		else
		{
			f=0;
			cnt=0;
		}
		if(cnt>max)
		{
			max=cnt;
		}
	}
	printf("%d",max);
	return max;

}

- Charles July 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h> 
int 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; 
}

- leela July 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anonymous August 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void consecutive1(unsigned int n)
{
int i,c=1;

for(i=0;i<30;i++)
{
if( (n&(1<<i)) && ( n&(1<<(i+1)) ) )
c++;
}

printf("times consecutive 1s %d",c);

}

- Anonymous August 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

c=0;
big=0;
for(num;num!=0;num>>1)
{
if(num&&1==1)
{
c++;
if(c>big)
big=c;
}

else
{
c=0;
}
}

- Vineet August 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main(void) {
unsigned int num=67;
unsigned char max=0;
unsigned char count=0;

while(num) {
if( n & 0x01)
count++;
else
count = 0;
n>>= 1;
if (count>max) max=count;
}
return(max);
}

- Rengith August 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- pankajkaushal1992 August 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

}

- vathsala September 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

}

- vathsala September 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anonymous September 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- kshafiee September 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- ks September 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- kshafiee September 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- kshafiee September 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- kshafiee September 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- chavandp7 November 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int getCount(int nNum)
{
	int nCount   = 0;
	int nMax     =0;
	for(int nI=0;nI<32;nI++)
	{
		if(0!=(nNum & 1<<nI))
		{
		  nCount++;	 
		  if(nCount>nMax)
		  {
		 	nMax=nCount;
	      }
		}
		else
		{
		 nCount =0;	
		}
	}
	return nMax;
}

- dreamBlitzer March 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int a = 0b10010101;
        int counter = 0;
        while (a)
        {
                if (((a >> 1)&1) & (a&1))
                        counter ++;

                a >>= 1;
        }
	print counter;

- Shiva Kumar Ganthi July 02, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int a = 0b10010101;
int counter = 0;
while (a)
{
if (((a >> 1)&1) & (a&1))
counter ++;

a >>= 1;
}
print counter;

- Shiva Kumar Ganthi July 02, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is my solution,

int a = 0b10010101;
int counter = 0;
while (a)
{
if (((a >> 1)&1) & (a&1))
counter ++;

a >>= 1;
}
print counter;

- Shiva Kumar Ganthi July 02, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

if we do n&(n-1) then rightmost set bit will reset so we will use this logic to count no of 1's

 int count = 0;

while(num != 0)
{
  count++;
  num = num & (num-1);
}

return count

- Kavita July 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

This will return the total number of set bits i.e. 1 bits in the number not consecutive set of 1 bits which is the question asked.

- vgeek July 04, 2014 | 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