Intuit Interview Question
Software Engineer in Testspublic static void main(String[] args) {
String v1 = "2.12.11.09";
String v2 = "2.12.11.09";
String[] v1Split = v1.split("\\.");
String[] v2Split = v2.split("\\.");
if (v1Split.length != v2Split.length)
System.out.println("Version Numbers not equal");
else {
for (int i = 0, j = 0; i < v1Split.length; i++, j++) {
if (!v1Split[i].equals(v2Split[j])) {
System.out.println("Version Numbers not equal");
return;
}
}
}
System.out.println("Version Numbers are equal");
}
We can split the version string using delimiter "[.]". Compare the string array from the result and as they are integers, each splitted string can be typecasted to integer and then compared.
Compare till you get the answer (till the two splitted strings are not equal).
i dont like posting the exact code.
This wont work. For example 2.06 and 2.6 would be equal as when you typecast "06" you will end up getting 6
import java.util.*;
import java.lang.*;
import java.io.*;
public class VersionCheck
{
public static void check (String s1, String s2)
{
String[] v1 = s1.split("\\.");
String[] v2 = s2.split("\\.");
int i=0;
int j=0;
while(i<=v1.length-1 && j<=v2.length)
{
int s = Integer.valueOf(v1[i]);
int r = Integer.valueOf(v1[i]);
if(Integer.valueOf(v1[i]) < Integer.valueOf(v2[j]))
{
System.out.println("the 2nd one is greater");
return;
}
else if(Integer.valueOf(v1[i]) > Integer.valueOf(v2[j]))
{
System.out.println("the 1st one is greater");
return;
}
else
{
i++;
j++;
}
}
System.out.println("they are equal");
}
public static void main (String[] args) throws java.lang.Exception
{
String a = "12.23.14";
String b= "12.23.14";
check(a,b);
}
}
This is a simple solution using String split and compareTo
import java.util.Arrays;
public class CompareVersionNumbers {
public static void main(String[] args) {
String version1 = "1.2.30.008";
// String version2 = "2.12.11.09";
String version2 = "1.2.30.08";
System.out.println(compareVersionNumbers(version1, version2));
}
// this method returns the smaller string out of the 2 parameters
private static String compareVersionNumbers(String version1, String version2) {
// You need to append . with a escape character to be able to split on basis of dot(.)
String[] version1point = version1.split("\\.");
String[] version2point = version2.split("\\.");
for(int i=0; i < version1point.length; i++){
if(i < version2point.length){
if(version1point[i].compareTo(version2point[i]) < 0){
return version1;
}
else if(version1point[i].compareTo(version2point[i]) > 0){
return version2;
}
}
else{
return version2;
}
}
//if the version1 parameter is smaller than version2 parameter
if(version1.length() != version2.length()){
return version1;
}
return "Both Strings are equal";
}
}
public static String compareVersion ( String str1 , String str2)
{
ArrayList list1 = new ArrayList();
ArrayList list2 = new ArrayList();
ArrayList list3 = new ArrayList();
ArrayList list4 = new ArrayList();
StringTokenizer tok1 = new StringTokenizer (str1,".");
while (tok1.hasMoreElements())
{
list1.add(tok1.nextToken());
}
StringTokenizer tok2 = new StringTokenizer (str2,".");
while (tok2.hasMoreElements())
{
list2.add(tok2.nextToken());
}
Integer len1 = list1.size();
Integer len2 = list2.size();
for (int i=0; i < len1 ; i++)
{
Integer temp = Integer.valueOf((String) list1.get(i));
list3.add(i, temp );
}
for (int i=0; i < len2 ; i++)
{
Integer temp = Integer.valueOf((String) list2.get(i));
list4.add(i, temp );
}
System.out.println(list3);
System.out.println(list4);
int max = Math.max(len1, len2);
for (int i=0 ; i < max ; i++)
{
if ((Integer)list3.get(i) > (Integer)list4.get(i))
return str1;
if ((Integer)list4.get(i) > (Integer)list3.get(i))
return str2;
}
return null;
}
public static String compareVersion ( String str1 , String str2)
{
ArrayList list1 = new ArrayList();
ArrayList list2 = new ArrayList();
StringTokenizer tok1 = new StringTokenizer (str1,".");
while (tok1.hasMoreElements())
{
list1.add(tok1.nextToken());
}
StringTokenizer tok2 = new StringTokenizer (str2,".");
while (tok2.hasMoreElements())
{
list2.add(tok2.nextToken());
}
Integer len1 = list1.size();
Integer len2 = list2.size();
for (int i=0; i < len1 ; i++)
{
Integer temp = Integer.valueOf((String) list1.get(i));
list1.remove(i);
list1.add(i, temp );
}
for (int i=0; i < len2 ; i++)
{
Integer temp = Integer.valueOf((String) list2.get(i));
list2.remove(i);
list2.add(i, temp );
}
int max = Math.max(len1, len2);
for (int i=0 ; i < max ; i++)
{
if ((Integer)list1.get(i) > (Integer)list2.get(i))
return str1;
if ((Integer)list2.get(i) > (Integer)list1.get(i))
return str2;
}
return null;
}
According to the JavaDocs for Java 6, "StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead."
The split method returns an array of strings, which has the advantage of quickly allowing you to compare the 2 lengths. Additionally, you can create a for loop to compare the individual strings, and only need to do conversion to Integer (or int) for those elements that aren't equal. (For this question, each piece would need the conversion.)
hum... just my two cents here...
- ronin February 06, 2011I'm not sure what the context of this question was. But sounds to me that they wanted to see a Class (Version) that besides the encapsulation of the version number it also inherits from Comparable interface.
Thus, making easy to compare with other via "compareTo()". And now you can use Collections to sort, searchBinary, etc.. and even you can use these objects as keys for TreeMap, TreeSet, etc.... so you'll end with more tricks just for free
Now the implementation of compareTo() it's a different story, we could argue here indefinitely till we get the perfect algorithm.. I'd say something with trees, perhaps Suffix trees?..