Amazon Interview Question for Software Engineer / Developers


Team: Instant Video
Country: United States
Interview Type: Phone Interview




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

unsigned int ip_str_to_int(char *str) {
unsigned int ip=0;

while(*str) {
ip = (ip *10) + (*str -'0');
*str++;
if (*str && *str++ == '.') //skip period
*str++;
}
return ip;
}

assuming passing valid four octet ip address string..u can add validation part before calling this function.

- shivacherukuri February 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

I think this can be easily done using bitwise operators

perl:

#!/usr/bin/perl
my ($a,$b,$c,$d) = split(/\./,"10.1.10.12");
print (($a<<24) | ($b<<16) | ($c<<8) | $d);

c:

#include<stdio.h>
#include<string.h>
int main()
{
unsigned ans=0;
char input[] = "10.1.10.12";
char *tok = strtok(input,".");
int i;
char delims[] = ".";
for(i=3;tok!=NULL;i--)
{
        ans |= atoi(tok)<<(8*i);
        tok = strtok(NULL,".");
}
printf("%u\n",ans);
}

- sylar February 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public double IPAddressToNumber(string IPaddress)
{
int i;
string [] arrDec;
double num = 0;
if (IPaddress == "")
{
return 0;
}
else
{
arrDec = IPaddress.Split('.');
for(i = arrDec.Length - 1; i >= 0 ; i --)
{
num += ((int.Parse(arrDec[i])%256) * Math.Pow(256 ,(3 - i )));
}
return num;
}
}

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

In .NET

const sampleIp = "255.255.255.255";
string toConvert = sampleIp.Replace(".", "");
uint ip = (uint)Int.TryParse(toConvert);

- Andrey February 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In C, with no error handling, and using built-in functions for string searching and decimal conversion:

typedef unsigned int ipaddr;

ipaddr parse_ipaddr(const char *str)
{
    ipaddr addr;
    while (str) {
        addr *= 256;
        addr += atoi(str);
        str = strchr(str, '.');
        if (str) str++;
    }
    return addr;
}

- psykotic February 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In c++:

long long ip2num(string s)
{
	short a,b,c,d;
	char t;
	long long ret = 0;
	stringstream ss(s);
	ss >> a >> t >> b >> t >> c >> t >> d;
	ret = ( a * 10e8 + b * 10e5 + c * 10e2 + d);
	return ret;

}

- SuYo February 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

index = binarySearch(intArray, searchValue);
		if (index != -1) {
			System.out.println("Found at index: " + index);
		} else {
			System.out.println("Not Found");

}

- mccrajs February 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ParseIPv4 {
	
	public static void main(String[] args){
		String ip = "192.168.0.1";
		ip = ip.replace(".","");
		int num = Integer.parseInt(ip);
		System.out.println(num);
	}

}

- mccrajs February 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

why not use sprintf?

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

#include <stdio.h>
#define STR1 "192.168.72.46"
#define STR2 ".192.168.72.46"
#define STR3 "192..72.46"
#define STR4 "192.72.46"
#define STR5 "192.168."
#define STR6 "192.168.72.46.23.44"
#define STR7 ".1923.168.72.46"
#define STR8 "192.168.#72.46"


unsigned long str_to_ip(char *str)
{

unsigned long ip = 0;unsigned long tmp = 0;char dotcount = 0, numcount = 0;
//First character cannot be anything ohter than
// '0' to '9'
if(*str > '9' || *str < '0')
return 0;
while( *str )
{

//subsequent characters cannot be anything other than
// '0' to '9' and '.'
if((*str > '9' || *str < '0') && (*str != '.')
/*&& (*str != '\0')*/){
//printf("returning from first if in while\n");
return 0;
}
else{
if(*str == '.'){
//following character cannot be anything other than
// '0' to '9'
if(*(str+1) > '9' || *(str+1) < '0')
return 0;
if(numcount > 3)
return 0;
dotcount++;numcount = 0;
str++;
ip |= tmp << ((4 - dotcount)*8);
//printf("tmp = %ld dot count = %d\n", tmp << 24, dotcount);
//printf("IP = %ld\n", ip);
tmp = 0;
}
else{
tmp = (tmp * 10) + (*str - '0');
//printf("tmp = %ld num count = %d\n", tmp, numcount);
str++;
numcount++;
}

}
if(dotcount > 3)
return 0;
}

if(dotcount < 3)
return 0;
ip |= tmp ;
//printf("tmp = %ld dot count = %d\n", tmp, dotcount);
//printf("IP = %ld\n", ip);
return ip;
}

int main()
{
int i;
unsigned long ip = 0;
printf("Testing function to convert IPv4 addr in string form to integer\n");
printf("Input : %s Output : %ld\n", STR1, ip = str_to_ip(STR1));
printf("Input : %s Output : %ld\n", STR2, ip = str_to_ip(STR2));
printf("Input : %s Output : %ld\n", STR3, ip = str_to_ip(STR3));
printf("Input : %s Output : %ld\n", STR4, ip = str_to_ip(STR4));
printf("Input : %s Output : %ld\n", STR5, ip = str_to_ip(STR5));
printf("Input : %s Output : %ld\n", STR6, ip = str_to_ip(STR6));
printf("Input : %s Output : %ld\n", STR7, ip = str_to_ip(STR7));
printf("Input : %s Output : %ld\n", STR8, ip = str_to_ip(STR8));
}

- Nitin Mahajan February 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char *str = "192.168.100.2"

for( ctr = 0 ; ctr < 4 ; ctr++ ) {
      value = atoi( strtok( ( ctr == 0 ) ? str : NULL , "." ) );
      res = res + ( value << ( 24 - ( ctr * 8 ) ) );
}

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

#include <stdio.h>

int main (int argc, char **argv)
{
    unsigned int  octet = 0,ip_addr = 0;
    char *str_ip, str[256];

    str_ip = str;
    printf("Please provide IPV4 address in string format \n");
    scanf("%s", str_ip);
    printf("IP address: %s\n", str_ip);

    while (*str_ip) {
        octet = octet * 10 + (*str_ip - '0');
        printf("str %c octet 0x%x\n", *str_ip, octet);
        str_ip++;
        if ((*str_ip == '.') || (*str_ip == 0)) {
            printf("ip_addr 0x%x\n", ip_addr);
            ip_addr =  (ip_addr << 8) | octet;
            printf("ip_addr 0x%x\n", ip_addr);
            octet = 0x0;
            if (*str_ip)
                str_ip++;
        }
    }
    printf("IP Address Hex: 0x%x, network order 0x%x\n", ip_addr, htonl(ip_addr));
}

- AG November 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

really usefull brother

- sathish May 22, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Removed debug statements...

#include <stdio.h>

int main (int argc, char **argv)
{
    unsigned int  octet = 0,ip_addr = 0;
    char *str_ip, str[256];

    str_ip = str;
    printf("Please provide IPV4 address in string format \n");
    scanf("%s", str_ip);
    printf("IP address: %s\n", str_ip);

    while (*str_ip) {
        octet = octet * 10 + (*str_ip - '0');
        str_ip++;
        if ((*str_ip == '.') || (*str_ip == 0)) {
            ip_addr =  (ip_addr << 8) | octet;
            octet = 0x0;
            if (*str_ip)
                str_ip++;
        }
    }
    printf("IP Address Hex: 0x%x, network order 0x%x\n", ip_addr, htonl(ip_addr));
}

- AG November 12, 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