Amazon Interview Question for Software Engineer Interns


Country: United States
Interview Type: Phone Interview




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

public static int cmpVersion(String ver1,String ver2){
        String [] sarr1=ver1.split("\\.");
        String [] sarr2=ver2.split("\\.");
        for(int i=0;i<sarr1.length;i++){
            int val1=Integer.parseInt(sarr1[i]);
            int val2=0;
            if(sarr2.length>i){
                val2=Integer.parseInt(sarr2[i]);
            }
            if(val1!=val2 ){
                return ((val1-val2)>0) ? -1 : 1;
            }
        }
        return 0;
    }

- manish.ceg January 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if string ver1 is shorter than string ver2?

- soysauce January 15, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int compareVersions(String v1, String v2){
		String[] sp1 = v1.split("\\.");
		String[] sp2 = v2.split("\\.");
	    int min = sp1.length>sp2.length?sp2.length:sp2.length;
	    for(int i=0; i<min; i++){
	    	int n1 = Integer.parseInt(sp1[i]);
	    	int n2 = Integer.parseInt(sp2[i]);
	    	if(n1==n2)
	    		continue;
	    	else{
	    		return (n1-n2)>0?-1:1;
	    	}
	    }
	    if(sp1.length!=sp2.length)
	    	return sp1.length>sp2.length?-1:1;
	    else
	    	return 0;
	}

- Prasanth January 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

int min = sp1.length>sp2.length?sp2.length:sp1.length;

- Anonymous January 15, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int compareVersion(String v1, String v2){
String v1_ = v1.replaceAll("\\D", "");
String v2_ = v2.replaceAll("\\D", "");
if (v1_.isEmpty() && !v2_.isEmpty()) return 1;
else if ( !v1_.isEmpty() && v2_.isEmpty()) return -1;
else if ( v1_.isEmpty() && v2_.isEmpty()) return 0;	// invalid inputs
return Integer.parseInt(v1_)>Integer.parseInt(v2_) ? -1 : 1;
}

- tango January 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static int compareVersion(String ver1, String ver2) {
		String[] str1 = ver1.split("\\.");
		String[] str2 = ver2.split("\\.");
		int loopCount = str1.length > str2.length ? str1.length : str2.length;
		if (!str1[0].isEmpty() && !str2[0].isEmpty()) {
			for (int i = 0; i < loopCount; i++) {
				int v1 = Integer.parseInt(str1[i]);
				int v2 = Integer.parseInt(str2[i]);
				if (v1 == v2)
					continue;
				else if (v1 > v2) {
					int v11 = Integer.parseInt(str1[i].substring(0, 1));
					int v21 = Integer.parseInt(str2[i].substring(0, 1));
					if (v11 >= v21)
						return -1;
					else
						return 1;
				} else if (v2 > v1) {
					int v11 = Integer.parseInt(str1[i].substring(0, 1));
					int v21 = Integer.parseInt(str2[i].substring(0, 1));
					if (v21 >= v11)
						return 1;
					else
						return -1;
				}
			}
		} else if (str1[0].isEmpty() && str2[0].isEmpty()) {
			return 0;
		} else if (str1[0].isEmpty())
			return 1;
		else if (str2[0].isEmpty())
			return -1;
		else
			return 0;
		return 0;
	}

- ibayrak January 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can remove all the dots except the first dot. This will result in a floating point number. We can directly do the fp comparison.

- Vijay January 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

1.12.3 and 1.1.3

- fw454@nyu.edu January 27, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

package zhang.juanyong.algorithms2;

import java.util.Queue;

import org.apache.commons.lang.StringUtils;

public class VersionComparator {
	public static int compareVersion(String v1, String v2) {
		Queue<Integer> q1 = new java.util.LinkedList<Integer>();
		Queue<Integer> q2 = new java.util.LinkedList<Integer>();
		loadVersion(q1, v1);
		loadVersion(q2, v2);

		int comp = 0;
		while (!q1.isEmpty()) {
			comp = q1.poll().compareTo(q2.poll());
			if (0 != comp) {
				return comp;
			}
		}

		return comp;
	}

	private static void loadVersion(Queue<Integer> q, String version) {
		String[] vNums = StringUtils.split(version, ".");
		for (String v : vNums) {
			q.add(Integer.valueOf(v));
		}
	}

	public static void main(String[] args) {
		String v1 = "10.3.4";
		String v2 = "10.3.41";
		System.out.println(String.format("compareVersion(%s, %s)=%s", v1, v2,
				compareVersion(v1, v2)));
	}

}

- QAQ May 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Many of these cases fall short for irregular inputs such as 10.0 < 10.0.1

Rather than cast to an integer, my example uses string comparison (character by character comparison like strcmp in c) to determine which number is larger.

import java.util.*;
public class Version {
	public static int which(String version1, String version2) {
		String[] versionParts1 = version1.split("\\.");
		String[] versionParts2 = version2.split("\\.");
		int maxLength = Math.max(versionParts1.length, versionParts2.length);
		int compare = 0;
		for(int i=0; i < maxLength && compare==0; i++) {
			// version 2 is an extended version of version1
			if(versionParts1.length==i) {
				compare=1; 
			// version 1 is an extended version of version2
			} else if(versionParts2.length==i) { 
				compare=-1; 
			} else {
				compare = versionParts2[i].compareTo(versionParts1[i]);
			}

		}
		return Integer.signum(compare); 
	}
	public static void main(String[] args) {
		System.out.println(which(args[0], args[1]));
	}
}

- Vartan March 15, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String v1=version1.replace(".","");
        String v2 =version2.replace(".","");
        if (Integer.parseInt(v1)==Integer.parseInt(v2)){
            System.out.println("0");
        }else
        System.out.println(Integer.parseInt(v1)>Integer.parseInt(v2)?"-1":"+1");

- Anonymous June 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String v1=version1.replace(".","");
        String v2 =version2.replace(".","");
        if (Integer.parseInt(v1)==Integer.parseInt(v2)){
            System.out.println("0");
        }else
        System.out.println(Integer.parseInt(v1)>Integer.parseInt(v2)?"-1":"+1");

- Anonymous June 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String v1=version1.replace(".","");
        String v2 =version2.replace(".","");
        if (Integer.parseInt(v1)==Integer.parseInt(v2)){
            System.out.println("0");
        }else
        System.out.println(Integer.parseInt(v1)>Integer.parseInt(v2)?"-1":"+1");

- sunny June 08, 2015 | 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