Domanda di colloquio di Amazon
I was asked two questions.
Q 1. You are given two version numbers of a software, like Version 10.3.4 and Version 10.3.41. Write a program to find out which of the version numbers are the latest. If version 1 is latest output -1, if version number 2 is latest output +1 else output 0 if same version. Both the version numbers are taken as string. He also asks to make the program of minimum time complexity as we can. At the end he also asked the difference between an iterative program and one with recurrence and their advantages and disadvantages.
Q 2. Given two files with a list of application IDs (or some kind of data) stored in them , write a program to compare the data in the two files and output all the common data found in each. What data structure would you use and why ? Give a minimum time and space complexity algorithm. Why did you choose the particular data Structure or algorithm ?
Risposte di colloquio
Why not just delimit the string with "."
Then you'll have an array of string numbers.
Then loop with the array from 0 -> n (depending on how many decimals there was)
and compare. Return when one is greater than the other.
You will have to check if you get a index out of bounds if one has more decimals than the other.
Over all, that should be in n time.
If you are using c++, can't you just use the "compare" method that is built into the string class?
Maybe not the best solution... the time complexity is O(n).
public static void main(String[] args) {
String ver1 = "10.22.7.3";
String ver2 = "10.22.8.4.1";
String split1[] = ver1.split("\\.");
String split2[] = ver2.split("\\.");
int max = split1.length > split2.length ? split1.length : split2.length;
for (int i = 0; i i) {
System.out.println(+1);
break;
} else if (split2.length i) {
System.out.println(-1);
break;
} else {
if (Integer.parseInt(split1[i]) > Integer.parseInt(split2[i])) {
System.out.println(-1);
break;
} else if (Integer.parseInt(split1[i]) < Integer
.parseInt(split2[i])) {
System.out.println(+1);
break;
}
}
}
}
String v1="10.3.4";
String v2="10.3.41;
double v12=v1.parseDouble();
double v22=v2.parseDouble();
If(v12>v22)
{ System.out.print(-1);}
else if(v22>v12)
{System.out.print(1);}
else{ System. out. print(0);}
Q:2
List a= new LinkedList();
List b= new LinkedList();
while(a.hasNext())
{
( int =1; i
As for #2. HashTable it. Also n time.
1. This was a little tricky. You have to check for the decimal place in both the versions and then Store that part of the string till the decimal place in a temporary variable and then convert them to integer and check which one is greater. You have to keep on doing that till you reach the end of the version numbers or till when you find the answer
2. In the second question you can use Linked lists and then Binary Search Algorithm. The interviewer will ask you the reason for this.