Arista Networks Interview Question


Team: platform engineering intern
Country: United States
Interview Type: Phone Interview




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

int mult(char a, unsigned char b) {
  int r = 0;
  while (b) {
    if (b & 1) r += a;
    a = a << 1;
    b = b >> 1;
  }
  return r;
}

Can be refined to handle negative numbers and overflow...

- david.rebatto@gmail.com March 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

recursive version

short int multiplywithoutstar(  char a, unsigned char b)
{
	if(b>1)
	{
		return ((b&1)?a:0)+((multiplywithoutstar(a,b>>1))<<1);
	}
	else if(b==1)
		return a;
	else return 0;
}

- Charles December 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use shift with power of two. Otherwise, add the number multiple times

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

you r absolutely right!!!

- unknown March 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

combine the 2 methods.. get to nearest power of 2 by shifting, then add/subtract.

- armudu March 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main()
{
int i=10,c=0,j=4;
while(j)
{
c=c+i;
j-=1;
}
printf("%d",c);
}

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

#include <iostream>
using namespace std;

int main(void){
/* find a*b */
int a = 3;
int b = 123;
int shift = 0;
int target = b;
int prod = 0;

while(target>0){
shift = 0;
while(1<<shift <= target){
shift++;
}
shift--;

prod += a<<shift;
target -= 1<<shift;
}

std::cout<< " "<<prod;
return 0;
}

- ayon November 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int multiply (int a, int b) {
	int res;
res = a << (b/2);
if (b%2)
	res = res + a;
return res;
}

- value October 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

int mult(int n1, int n2)
{
int res = 0;
const int mask = (0x1 << (sizeof(int)*8-1));

while (n1 != 0) {
if (n1 & mask)
res += n2;
res <<= 1;
n1 <<= 1;
}

return res;
}

- bond June 02, 2012 | 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