Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

stringstream strin;
     string ans, valstr;
     int shift = 24;
     for (int i=0; i < 4; i++)
     {
           int val = (num >> shift) &(0xFF); shift -= 8;
           stringstream strin;
           strin << val;
           strin >> valstr;
           ans.append(valstr);
           if (shift>=0) ans.append('.'); 
           
     }

- Anonymous July 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

thanks

- manish27.p July 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

is there any method apart from bitwise method.

- manish27.p July 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

string ipaddress(int num){
        char buffer[37];
        sprintf(buffer, "%d.%d.%d.%d", (num>>24)&0xFF, (num>>16)&0xFF, (num>>8)&0xFF, (num&0xFF));
        return string(buffer);

}

- Anonymous July 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

buffer[16] is enough by the way.

- Ambu Sreedharan July 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is Java code converting using strings

String stringIPAd = String.format("%1$08x", 2314105025L);
		Integer First = Integer.parseInt(stringIPAd.substring(0,2),16);
		Integer Second = Integer.parseInt(stringIPAd.substring(2,4),16);
		Integer Third = Integer.parseInt(stringIPAd.substring(4,6),16);
		Integer Fourth = Integer.parseInt(stringIPAd.substring(6,8),16);
		
		
		System.out.print(First.toString()+".");
		System.out.print(Second.toString()+".");
		System.out.print(Third.toString()+".");
		System.out.println(Fourth.toString());

- Jim July 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the approach I took in Java:

package myUtil;

import java.util.Random;

public class IpAddrToString {

  public static String intIpToString (int ipAddr) {

    StringBuilder ipAddrStr = new StringBuilder();
    for (int i=1; i<=3; i++) {
      ipAddrStr.insert(0, "." + String.valueOf(ipAddr & 0xFF));
      ipAddr = ipAddr >> 8;
    }
    ipAddrStr.insert(0, String.valueOf(ipAddr & 0xFF));
    return ipAddrStr.toString();

  }
  
  public static void main(String[] args) {
    
    Random rand = new Random(System.currentTimeMillis());
    int ipAddr = rand.nextInt();
    System.out.println("The string IP for int [" + ipAddr + "] is [" + intIpToString(ipAddr) + "]");

  }
    
}

Note: I elected to use ">>>" (shift in 0's on left side) instead of ">>" (shift in msb on left side) to keep things "clean", though in this case it does not matter (at least functionally, and I assume that ">>>" performs at least as well as ">>", but am not sure of that).

Sample test runs yield:
The string IP for int [262875931] is [15.171.43.27]
The string IP for int [647301379] is [38.149.9.3]
The string IP for int [-1484977025] is [167.125.12.127]

I believe that the negative value in the last example is simply a byproduct of all Java ints being signed.

- Darren Gardner July 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include <stdio.h>
#include <string.h>

main()
{
    int ip[]={1,1,1,1,1,1,1,1,0,0,1,0,1,1,1,0};
    int part = 0;
    int i;
    int j;
    for (i = 0; i < 16; i+=4)
    {
        part = 0;
        for(j = i; j < 4; j++)
        {
            part = (part << 1)  | ip[j];
        }
        printf ("%x", part);
    }
    return 0;
}

What is the problem with this code ??.

- rohith July 31, 2013 | 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