## Interview Question

## Interview Answer

9 Answers

How about getting the ascii code for each character, substracting 48, then you would have each digit. Then you can multiply them?

1. Add using a loop.

Or

2. Bitwise operation.

// C#

static int StrToInt( string s )

{

if( s == null ) return int.MinValue;

int n = 0;

for( int i = s.Length - 1; i >= 0; --i )

n = (n * 10) + (s[i] - '0');

return n;

}

static int MultiplyStrings( string s1, string s2 )

{

int n1 = StrToInt( s1 );

int n2 = StrToInt( s2 );

return n1 * n2 ;

}

public String multiply(String one, String two)

{

char[] one_char;

char[] two_char;

int carry = 0;

if(one.length() > two.length()){

one_char = one.toCharArray();

two_char = two.toCharArray();

}

else{

one_char = two.toCharArray();

two_char = one.toCharArray();

}

int[] result = new int[one_char.length + two_char.length];

for(int index = two_char.length - 1; index >= 0 ;index--){

for(int index2 = one_char.length - 1; index2 >= 0 ;index2--){

int value = ((one_char[index2] - '0') * (two_char[index] - '0')) + result[(index2 + index) + 1];

result[(index2 + index) + 1] = value % 10;

carry = value / 10;

result[(index2 + index)] += carry;

}

}

return Arrays.toString(result);

}

public static void main(String[] args) {

String s1 = "4567";

String s2 = "1234";

int res = convertString2Int(s1) * convertString2Int(s2);

System.out.println(res);

}

public static int convertString2Int(String s){

char []chs = s.toCharArray();

int n1 = 0;

for(int i=0; i < chs.length ; i++){

n1 = n1 + (chs[i]-'0') * getPowerOf(10, chs.length-i);

}

return n1;

}

public static int getPowerOf(int num, int times){

int val = 1;

for(int i =0; i<times-1; i++){

val = val * num;

}

return val;

}

God damn these commentors are idiots..it clearly says that you cannot convert to an int and you paste pieces of memorized code where you are converting to int..smh

Subtracting char '0' is also what I first thought of but, If the restriction is not using any conversion to integers then one possible way would be to code up multiplication and addition tables (e.g., a dictionary of dictionaries would allow fast indexing into the result) and then using the algorithm from elementary school (or, if you feel really fancy, do something like Karatsuba multiplication http://en.wikipedia.org/wiki/Karatsuba_algorithm)

//Assumption +ve interger ... no sigh, no .,no float resut is within range of int

int multiplicationWithoutCast(string str1,string str2)

{

int finalresult=0;

if(str2.length() == 1)

{

int result=0;

int intResult =0;

int carry = 0;

int j = 1;

// used unsigned int i so infinite loop bcz never goes below zero

for(int i=str1.length()-1; i>=0; i--,j=j*10) // u did j+10 first :/

{

intResult = (str1[i] - '0' ) * (str2[0] -'0') + carry;

//cout << intResult << " ";

carry = intResult/10;

intResult = (intResult%10) * j;

result = intResult + result;

//cout << carry << " " << intResult << " " << result << endl;

}

if(carry)

result = result + carry * j;

return result;

}

else

{

for(int i=str2.length()-1,j=1;i>=0;i--,j=j*10)

{

finalresult = finalresult + multiplicationWithoutCast(str1,str2.substr(i,1)) * j;

}

return finalresult;

}

}

## Add Answers or Comments

To comment on this, Sign In or Sign Up.

I tried many methods but was not successful or/and efficient