Intuit Interview Question for Software Engineer in Tests






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

hum... just my two cents here...

I'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?..

- ronin February 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public 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");
}

- Anuj Kulkarni October 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

- Hinax December 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This wont work. For example 2.06 and 2.6 would be equal as when you typecast "06" you will end up getting 6

- vamsi February 21, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this wont work. for exmple 2.06 and 2.6 will be same as when you typecast "06" you will end up getting 6

- krishna32 February 21, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am not sure how split is good because, if the string is very large, then storing parts of it might overflow the memory and regex is obviously very slow. Better way will be to find the index of . and keep comparing.

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

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);
	}
		
	
}

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

while(i<=v1.length-1 && j<=v2.length-1) *

- Shruthi October 15, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this solution wont work for inputs like 2.06 and 2.6

- krishna32 February 21, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

we can fix this by adding decimal point to the string in the front -
Integer.parseInt("0.06") and Integer.parseInt("0.6"), this way 2.6 will be greater than 2.06

- krishna32 February 21, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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";
	}
}

- Amit November 01, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

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;
}

- ugaach January 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

very poor code... dont you know something like String[] str = string.split(characterRegEx) ??

- Anuj Kulkarni October 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

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;
}

- ugaach January 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.)

- aswietlicki January 09, 2011 | Flag


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