Cisco Systems Interview Question for Software Engineer / Developers


Team: Switching Softwares
Country: United States
Interview Type: Phone Interview




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

Set the range of bits from #L (least) to #H(highest)
1. Set the bit #(H-L+1)
2. Subtract 1 from the result
3. Shift the result by L bits to right
Just omitting the corner case checks to make the code clean.

#include <stdio.h>
int set_bits_range(int start, int end)
{
    return ( (1 << (end - start + 1)) - 1 ) << start;
}
int main (int argc, char *argv[])
{
    int rv = 0;
    rv = set_bits_range(3, 5);
    printf("%d\n", rv);
    return 0;
}

- ashot madatyan May 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Since index is starting from 1.

int set_bits_range(int start, int end)
{
    return ( (1 << (end - start + 1)) - 1 ) << (start-1);
}

- Psycho May 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

int set_bits(int start, int end) 
{ 
 return (1<<end) - (1<<(start-1)) ; 
} 

example set_bits(2,8) = 254 
 		set_bits(2,4) = 14 
 		set_bits(1,3) = 7

This is essentially simplification of return ( (1 << (end - start + 1)) - 1 ) << (start-1);

- Rama Chandra Reddy T November 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

What about the below code, please give feedback

int setbits(int start, int end) {
int no = 0;
int i;

for(i = start -1 ; i <= end - 1; i++) {
no |= (1 << i);
}

return no;

}

- Mohamed Mustafa March 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

works

- coolpyro April 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

number |= (((1 << end+1) - 1) ^ ((1 << start)-1));

- Abhinav April 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Previous answer is when LSB starting at 0
For LSB as 1:

number |= (((1 << end) - 1) ^ ((1 << start-1)-1));

- Abhinav April 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<math.h>
int createint(int startbit, int endbit)
{
int finalint = 0;
int i,orint;
for(i=startint; i <= endbit; i++)
{
orint = (int)pow(2,i);
finalint = finalint | orint;
}
return finalint;
}

- candidate March 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

unsigned int SetBits(unsigned int Num, unsigned int From, unsigned int To)
{
return (Num | (((unsigned int)~0 >> (32 - (To - From + 1))) << From));
}

- Guy March 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;

int a[4];

void createint(int startbit, int endbit)
{
for(int i = startbit; i <= endbit; i++)
a[4-i] = 1;
}

int main()
{
for(int i = 0; i < 4; i++)
a[i] = 0;
createint(2,4);
for(int i = 0; i < 4; i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}

- Candida March 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

2^(end-start )<<start

- Anonymous April 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

2^(end-start )-1<<start

- Anonymous April 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
if start=2 and end =6 ,index starting from 1 then result should be 111110 {{{ { int num=0,start=2,end=6,i,result=0; for(i=start-1;i<end;i++) // if you start index from 0 then i=start num+=pow(2,i); result=result|num; printf("%d",result); return 0; } - mangal.govind April 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
if start=2 and end =6 ,index starting from 1 then result should be 111110 {{{ { int num=0,start=2,end=6,i,result=0; for(i=start-1;i<end;i++) // if you start index from 0 then i=start num+=pow(2,i); result=result|num; printf("%d",result); return 0; } - mangal.govind April 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if start=2 and end =6 ,index starting from 1 then result should be 111110

{
 int num=0,start=2,end=6,i,result=0;

for(i=start-1;i<end;i++) // if you start index from 0 then i=start 
 num+=pow(2,i);

result=result|num;
printf("%d",result);
return 0; 
}

- mangal.govind April 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if start=2 and end =6 ,index starting from 1 then result should be 111110

{
 int num=0,start=2,end=6,i,result=0;

for(i=start-1;i<end;i++) // if you start index from 0 then i=start 
 num+=pow(2,i);

result=result|num;
printf("%d",result);
return 0; 
}

- mangal.govind April 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int setBit(int start, int end)
{
int val = 1, val1;

end -= start;

while (start--) val <<=1;

while (end--) {
val1 = val;
val <<=1;
val = val | val1;
}

return val;
}

- Genisim April 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int setbits(int start, int end) {
int no ;
int i;
int y = pow(2,end+1-start);
y = y-1;
printf("%d \n",y);
no = y<<(start-1);
return no;

}

- softy June 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int setBit(int start, int end)
	return (1<<end) - (1<<start) - 2;

- Jason October 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int set_bits(int start, int end)
{
return (1<<end) - (1<<(start-1)) ;
}

example set_bits(2,8) = 254
set_bits(2,2) = 2
set_bits(1,3) = 7

- Rama Chandra Reddy T November 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int set_bits(int start, int end)
{
return (1<<end) - (1<<(start-1)) ;
}

example set_bits(2,8) = 254
set_bits(2,2) = 2
set_bits(1,3) = 7

- Rama Chandra Reddy T November 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Cisco gives hike once in 2-3 years.

New-Joinees only get hike after 2-3 years, so take 100% hike at the time of joining .. . otherwise don't cry after joining. :)

- Simple April 25, 2014 | 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