Riverbed Interview Question for Software Engineer / Developers


Country: United States




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

here is an intelligent way(i guess..:P) to do it: I hope this helps!
#include<stdio.h>

union ip {

   unsigned int int_val;
   unsigned char c[4];
};


int
 main()
 {
   union ip p;
   p.int_val = 3232235786; //int val for 192.168.1.10
   
   printf("%u.%u.%u.%u\n", p.c[3], p.c[2], p.c[1], p.c[0]);
 
   return 0;
 }

- raja roy January 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Seems like a clever solution. Is it guaranteed to be portable? I don't really use unions (most people don't, I think), so I don't know what exact guarantees they provide. Could some machine or compiler, for instance, decide to make the union more than 4 bytes and have the bytes of the int not line up with the bytes of the char[]?

- eugene.yarovoi January 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

well implementation of unions are standard over by all compliers (as far as I know)even the kernel(linux) uses it a lot.

- raja roy January 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

can you please tell me how it works?

- own March 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

public String getIPAddress(int IPAddress) {
    if(IPAddress < 0) return null;
    int fourth = IPAddress % 256;
    IPAddress = IPAddress / 256;
    int third = IPAddress % 256;
    IPAddress = IPAddress / 256;
    int second = IPAddress % 256;
    int first = IPAddress / 256;
    return String.format("%d.%d.%d.%d", first, second, third, fourth);
}

(Sorry, not very comfortable with C/C++ :()

- vijay January 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the last part of your solution is a bit messed up :

int second = IPAddress % 256;
IPAddress = IPAddress / 256;
int first = IPAddress % 256;

- 1wanna March 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

char * get_ip_address(int ip_address)
{
    unsigned int ip = (unsigned int) ip_address;
    char* bytes = (char*) malloc(4*sizeof(*bytes));
    bytes[0] = (char) (ip_address >> 24);
    bytes[1] = (char) (ip_address >> 16);
    bytes[2] = (char) (ip_address >> 8);
    bytes[3] = (char) ip_address;
    return bytes;
}

This would really take an unsigned int and return an unsigned char* if I got to pick...

- eugene.yarovoi January 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@eugene.yarovoi: Your method doesn't "basically convert input ip_address to user-friendly format a.b.c.d" as asked in the question.

- vijay January 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Fair enough. I supposed getting the bytes was the important part. How's this:

char * get_ip_address(int ip_address)
{
    unsigned char bytes[4];
    bytes[0] = (unsigned char) (ip_address >> 24);
    bytes[1] = (unsigned char) (ip_address >> 16);
    bytes[2] = (unsigned char) (ip_address >> 8);
    bytes[3] = (unsigned char) ip_address;

     //up to 3 chars for 4 groups of digits, 3 dots, and 1 null char
    char* out = (char*) malloc((3*4+3+1)*sizeof(*out));
    sprintf (out, "%u.%u.%u.%u", bytes[0], bytes[1], bytes[2], bytes[3]);
    return out;
}

- eugene.yarovoi January 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i don't think it works.
suppose the input is 192168110. the output should be 192.168.1.10 but not 0.192.168.110.
you are assuming that the given ip is already in a (4 x 3-digits = 12 digits) format. but i don't think this is the intention of the interviewer: otherwise they could just ask you to separate a 12-digit int.

- yvette January 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No. That's not what this code is even supposed to do. 192168110 won't become 0.192.168.110 with my code, nor is 192.168.1.10 the right answer. The 4 bytes of the IP address are packed into the int, not just stored as back-to-back decimal digits.

- eugene.yarovoi January 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

can you please explain me the logic of this.

thank you.

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

I'd be happy to explain it to you. Which part did you need explained?

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

One more thing to add to this, what about the endian-ness of the system , i mean how will you now that an ip address will convert to 192.168.1.2 or 2.1.168.192

We use ntohl() and htonl(), these are ought to be used.

- Aseem Vyas January 15, 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