## Microsoft Interview Question

• 0

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

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

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

Lol

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

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

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

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.

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

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.

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

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

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

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

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

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

xcellent..:)

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

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

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

why are you taking power? What are these?

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

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

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.

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

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

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

Bad Solution since strings can easily get out of integer range

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

}
}

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);
b= strlen(argv);
min= a < b ? a : b ;
for(i=0;i<min;i++){
if (argv[i] < argv[i]){
st=  strcat(argv,argv);
printf("%s",st);
return 0;
}
else if  (argv[i] > argv[i]){
st=  strcat(argv,argv);
printf("%s",st);
return 0;
}
// noted equal case ignored and later taken care of..
}

// if the argv, argv is prefix of one then this check is needed

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

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

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

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

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

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.

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

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.

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

same problem what if string gets out of integer range

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

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

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

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

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,s2,str1,str2;
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;
}``````

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

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.

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