Microsoft Interview Question


Country: India




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

Here is the solution.
1. Set String one = s1+s2
2. Set String two = s2+s1
3. Now if(one > two) return one+two else return two+one
Below is the java Code

String maxNumber(String s1, String s2){

            String one  = s1 + s2;
            String two  = s2 + s1;
            for(int i=0;i<one.length();i++){
                         if(one.charAt(i) > two.charAt(j))
                               return one;
                         else if(one.charAt(i) < two.charAt(j))
                               return two;
                    }
                    //If we come here means both are equal, so return anything
                     return one;
       }

- loveCoding October 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I cannot understand your solution

- Loler October 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Lol

- Loler October 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

your solution won't work in case of string which are -ve number and in case of strings which are not convertable to number.

- Anonymous October 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your solution is good.

Negative (Who the hell writes -ve to mean negative) numbers don't make sense in the context of this question so obviously that isn't what's being asked. Otherwise you'd have to worry about

a = -45
b = -456

If you concatenate those strings together you get the string "-45-456" which doesn't make any god damned sense as it's not asking you what string evaluates to the highest value.

Additionally pointing out that this doesn't work if the input values aren't "convertable to number" is unnecessary.

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

Very smart and simple solution, Manan. Unlike other solutions, there are no multiplies or other arithmetic that you are using. So, there is no chance of overflow.

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

what is j here?
Will it work when numbers are like 45 450, in which case if i understand your solution, it will return any but answer should be 45450 instead of 45045

- Bruce October 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

thought it's simple, it need to join string twice
this can be solved by join string once

- dgbfs November 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 5 vote

Suppose two string, stringA and stringB, are integer-convertible.

a = atoi(stringA.c_str());
b = atoi(stringB.c_str());
valA = a * power(10, stringB.size()) + b;
valB = b * power(10, stringA.size()) + a;

return valA > valB? valA : valB;

- peter tang October 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

xcellent..:)

- Anonymous October 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if length of string is long? say 10 or 15
multiplication & power doesn't really work

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

why are you taking power? What are these?

valA = a * power(10, stringB.size()) + b;
valB = b * power(10, stringA.size()) + a;

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

Your comment is correct if this string is really long.(out of 64-bit can take) But if it is down to that road, the first question to answer to how to store the really large number (data structure) in computer. For instance a number is composed of 1 billion digits. For some meaningful computing some sanity checking is necessary beforehand.

- peter tang October 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You just need to calculate largest of the 2 string as integer/double and the other one you need to append it to that.

ex:

string a="150";
string b="120";

string result = (Convert.ToInt32(a)>=Convert.ToInt32(b))?string.Concat(a,b):string.Concat(b,a);

- Vijay B October 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Bad Solution since strings can easily get out of integer range

- Loler October 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

public static String findMax(String s1, String s2)
{
int flag = 0;
if(s1 != null && s2!= null)
{
for(int i=0; i<s1.length(); i++)
{
if(s1.charAt(i) > s2.charAt(i))
{
flag = 1;
return s1+s2;
}
else if(s1.charAt(i) < s2.charAt(i))
{
flag = 2;
return s2+s1;
}

}
}

- Pankaj Gadge October 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

inputs are given as arguments.. e.g ./a.out 45 465

#include<stdio.h>
#include<string.h>
int main(int argc, char * argv[]){
int a, b,min,i;
char *st;
 
 a= strlen(argv[1]);
 b= strlen(argv[2]);
 min= a < b ? a : b ;
 for(i=0;i<min;i++){
     if (argv[1][i] < argv[2][i]){
             st=  strcat(argv[2],argv[1]);
              printf("%s",st);
              return 0;
     }
     else if  (argv[1][i] > argv[2][i]){
             st=  strcat(argv[1],argv[2]);
              printf("%s",st);
              return 0;
}
     // noted equal case ignored and later taken care of.. 
}

// if the argv[1], argv[2] is prefix of one then this check is needed

   if (a<b){
     if (argv[1][0] >= argv[2][min] )
          st= strcat(argv[1],argv[2]);
     else 
             st=  strcat(argv[2],argv[1]);
   }
   else { 
     if (argv[2][0] >= argv[1][min] )
          st= strcat(argv[2],argv[1]);
     else 
             st=  strcat(argv[1],argv[2]);
   }
     printf("%s",st);
     return 0;
}

- debjyotipaul385 October 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String a = "45";
String b = "4545456";

String merge_to_max (String a, String b) {

if (a.length() > b.length()) {
   String c = a;
    a = b;
    b = c;
}

for (int i=0; i<b.length(); i++) {
   int mod_a = i % a.length();
   if (a.charAt(i) > b.chartAt(i)) {
      return a + b;
   } else if (a.charAt(i) < b.charAt(i)) {
      return b + a;
   }
}

   return a+b;
}

- Anonymous October 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String a = "45";
String b = "4545456";

String merge_to_max (String a, String b) {

if (a.length() > b.length()) {
String c = a;
a = b;
b = c;
}

for (int i=0; i<b.length(); i++) {
int mod_a = i % a.length();
if (a.charAt(mod_a) > b.chartAt(i)) {
return a + b;
} else if (a.charAt(mod_a) < b.charAt(i)) {
return b + a;
}
}

return a+b;
}

- Anonymous October 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 4 vote

There is even a more cunning method. Since the comparison of number is the same as the string/ASCII. For instance 46465 < 46546, for string it is also very true that "46465" < "46545" because the ASCII of "5" is more than "4".

So we can append these two string in difference order, the one winning on string comparison wins in integer-conversion.

// StringA and StingB input
std::string combAB = StringA + StringB;
std::string combBA = StringB + StirngA;

return combAB.compare(combBA) > 0? atio(combAB) : atio(combBA);

- peter tang October 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Now you need to port your code to a machine that uses EBCDIC. Does your approach work? If not, then re-write it so that it works on an EBCDIC machine and a non-EBCDIC machine.

- Interviewer October 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It should not matter as long as the string comparison is implemented as if '4' < '5', and atio() does the same thing. Remember that we are not doing assembler.

- peter tang October 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

same problem what if string gets out of integer range

- Loler October 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <string.h>
#include <stdlib.h>
int max_sum(char *s1, char *s2)
{
char *num1,*num2;
int n1,n2;
num1 = s1;
num1 = (char*)malloc(strlen(s1) + strlen(s2) +1);
num2 = (char*)malloc(strlen(s1) + strlen(s2) +1);
memcpy(num1,s1,strlen(s1));
memcpy(num1+strlen(s1),s2,strlen(s2)+1);
memcpy(num2,s2,strlen(s2));
memcpy(num2+strlen(s2),s1,strlen(s1)+1);
n1 = atoi(num1);
n2 = atoi(num2);
free(num1);
free(num2);
if(n1 > n2)
return n1;
return n2;

}


int main()
{
max_sum("123","345");
}

- Piyush October 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming powers etc are not allowed (if the string length is too large!)

WLOG, assume a.length <= b. length. For example, if a.length>b.length, define c = b and d = a. And work with c and d.

Check bit by bit if
a[i] > b[i]

string compare(string a, string b)
{
int flagcount = 1;
string result;

for (i = 0, i<a.length && flagcount =1, i++)
{
    if(a[i] > b[i])
    {
        flagcount = 2;
        result = a+b;
    }

     if(a[i] = b[i])
     {
         flagcount = 1;
      }

    if(a[i]<b[i])
   {
       flagcount =0;
       result = b+a;
   }

}   

if (flagcount ==1)
{
string modb = b[a.length + 1 : b.length-1];
string moda = modb + a;
result = compare(moda, b);
}

return result;
}

- alphalpha October 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If the integer values of s1 and s2 are all larger than 32-bit, or 64-bit integer, what will happen?
Thus I think we should handle them as strings, instead of converting them to integers.

Below is my C++ code.
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

bool isLarger(char* s1, char* s2)
{
bool flag = false;
int l1 = strlen(s1), l2 = strlen(s2), l = l1 > l2 ? l1 : l2, i = 0;
char* f1 = new char[l];
char* f2 = new char[l];

for(i = 0; i < l1; f1[i] = s1[i], i++);
for(; i < l; f1[i++] = '9');
for(i = 0; i < l2; f2[i] = s2[i], i++);
for(; i < l; f2[i++] = '9');

for(i = 0; i < l; i++)
{
if(s1[i] < s2[i])
{
flag = true;
break;
}
}

delete f1;
delete f2;

return flag;
}

char* spellNumber(char* s1, char* s2)
{
int i = 0;
char* spell = new char[strlen(s1) + strlen(s2) + 1];

if(isLarger(s1, s2))
{
for(i = 0; i < strlen(s2); i++)
{
spell[i] = s2[i];
}
for(; i < strlen(s1) + strlen(s2); i++)
{
spell[i] = s1[i - strlen(s2)];
}
}
else
{
for(i = 0; i < strlen(s1); i++)
{
spell[i] = s1[i];
}
for(; i < strlen(s1) + strlen(s2); i++)
{
spell[i] = s2[i - strlen(s1)];
}
}
spell[i] = '\0';

return spell;
}

int main()
{
char* s1 = "1243";
char* s2 = "5634";
char* spell = spellNumber(s1, s2);

cout << spell << endl;

delete spell;

return 0;
}

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

{
String a="45",b="456";
int c=Integer.parseInt(a+b);
int d=Integer.parseInt(b+a);
if(c>d) System.out.println(c);
else System.out.println(d);
}

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

#include<iostream>   // Author : Jeevan B.Manoj,TKMCE
#include<stdlib.h>
#include<string.h>
using namespace std;
int main()
{
    char s1[60],s2[60],str1[30],str2[30];
    strcpy(s1,"");
    strcpy(s2,"");
    int n1,n2;
    cout<<"Enter S1 and S2 ";
    cin>>str1>>str2;
    strcat(s1,str1);
    strcat(s1,str2);
    n1=atoi(s1);
    strcat(s2,str2);
    strcat(s2,str1);
    n1=atoi(s2);
    if(n1>n2)
        cout<<"\n"<<s1;
    else
        cout<<"\n"<<s2;
    return 0;
}

- Jeevan B Manoj November 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

only join the string once

public String join(String a, String b)
	{
		for(int i = 0; i < a.length() + b.length(); i++) {
			char c1 = i < a.length() ? a.charAt(i) : b.charAt(i - a.length());
			char c2 = i < b.length() ? b.charAt(i) : a.charAt(i - b.length());
			if (c1 < c2)
				return b + a;
			else if (c1 > c2)
				return a + b;
		}
		return b + a;
	}

- dgbfs November 28, 2012 | 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