Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

If I could write this in COBOL, I would use the first variable and split this into an array of variables delimited by '.'. Then I would use a FOR loop to compare both the arrays and if I find that one is greater than the other I would break thge loop and come out and display that the answer

- Sri October 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

and why wouldn't a simple string comparison work?

- Algo Visa October 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Guys an ordinary string comparison would work for sure .. Coz I have tries comparing two dates

- Sri October 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int isBig(char *s1, char *s2) {
int i, f = 0;
if (!(s1[i] || s2[i]))
return f;

for(i = 0; s1[i] && s2[i]; i++) {
printf("%c %c ",s1[i], s2[i]);
if (s1[i] == '.' && s2[i] == '.') {
if (f)
return f;
}
else if (s1[i] == '.')
return -1;
else if (s2[i] == '.')
return 1;
else if (s1[i] > s2[i] && f == 0)
f = 1;
else if (s2[i] > s1[i] && f == 0)
f = -1;
printf("%d\n",f);
}
if (f)
return f;
else if (s1[i])
return 1;
else if (s2[i])
return -1;
return 0;
}

- nagaraju October 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry with braces:

int isBig(char *s1, char *s2) {
int i, f = 0;
if (!(s1[i] || s2[i])) {
return f;
}

for(i = 0; s1[i] && s2[i]; i++) {
if (s1[i] == '.' && s2[i] == '.') {
if (f) {
return f;
}
}
else if (s1[i] == '.') {
return -1;
}
else if (s2[i] == '.') {
return 1;
}
else if (s1[i] > s2[i] && f == 0) {
f = 1;
}
else if (s2[i] > s1[i] && f == 0) {
f = -1;
}
}

if (f) {
return f;
} else if (s1[i]) {
return 1;
} else if (s2[i]) {
return -1;
}
return 0;
}

- nagaraju October 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey21736" class="run-this">class Main {

public static int compareVal(String a, String b) {
int compareVal = a.compareTo(b);
if(compareVal == 0) {
return 0;
} else if(compareVal < 0) {
return -1;
} else {
return 1;
}
}

public static int compareVersions(String versionA, String versionB) {
String[] aParts = versionA.split("\\.");
String[] bParts = versionB.split("\\.");

int numComparisons = Math.min(aParts.length, bParts.length);
for(int i = 0; i < numComparisons; ++i) {
int compareVal = compareVal(aParts[i], bParts[i]);
if(compareVal != 0) {
return compareVal;
}
}
if(aParts.length < bParts.length) {
for(int i = numComparisons; i < bParts.length; ++i) {
if(Integer.parseInt(bParts[i]) != 0) {
return -1;
}
}
} else if(aParts.length > bParts.length){
for(int i = numComparisons; i < aParts.length; ++i) {
if(Integer.parseInt(aParts[i]) != 0) {
return 1;
}
}
}
return 0;
}

public static void main(String[] args) {
System.out.println(compareVersions("1", "1"));
System.out.println(compareVersions("1", "0"));
System.out.println(compareVersions("1", "1.0"));
System.out.println(compareVersions("1.0", "1"));
System.out.println(compareVersions("1.1", "1.1.0.1"));
}
}
</pre><pre title="CodeMonkey21736" input="yes">
</pre>

- smffap November 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

create bst with string and Do postoder traversal...hope this helps.

- topcoder May 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

except for the validation whether a string is a proper version string

- Algo Visa October 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Why wouldnt a simple string comparison work?

sum = str1[i] - str2[i];
if(sum == 0)
{
i++;
}
if(sum > 0) return 1;
if(sum < 0 ) return -1;


cases
str1[i] =1 str2[i] =1 -> sum =0;
str1[i] =1 str2[i] =2 -> sum = -1; meaning str2 is latest and vice versa
str1[i] =1 str2[i] = '.' ->sum = 3; str1 is line 'X1; and str2 is 'X.' meaning that
str1 is latest

- TheGame October 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In string comparison
1.11.1-1.1.1 may not work

- sunil October 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

String a = "1.11.1";
String b = "1.1.1";
b.compareTo(a)
returns +ve for b>a, -ve for a>b, 0 Otherwise.

- Marideth October 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

String comparision will not work. Something like multiplying each digit with power of x in that position will work. We can use x = 10

- kiran October 18, 2011 | 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