Kaseya Interview Question for Java Developers


Country: India
Interview Type: In-Person




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

Im nto sure what you mean by that a number is 10000 digits if theyre 10-digit numbers. But if you want to multiply numbers you can jsut do divide and multiply.

pseudocode'ish:

def multiply (a,b):
  res = 0
  while a > 1:  
    if a & 1 == 1:
      res += b
    else:
      a = a >> 1
      res *= 2
  return res

- Anonymous September 13, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

and of course the first if branch should have a -= 1

- Anonymous September 13, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

meaning of "a number is 10000 digits " is that it can not be stored in int variable OR long variable

it should be stored in string and need to do the multiplication for each digit that is stored in the form of chars in number string and add all the multiplication to get final result

- RK September 25, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Scanner;
public class LargeMultiply {
// main method
public static void main(String args[]) {
System.out.println("\n\t \t WELCOME TO OneArmed Blog !!! \n");
System.out.println("\t \t Multiplying 2 Large Numbers \n");
Scanner sc = new Scanner(System.in);
System.out.println("Enter 1st Number:"); //input first number
String inputA = sc.next(); // taking string input
System.out.println("Enter 2nd Number:"); //input second number
String inputB = sc.next(); // taking string input
LargeMultiply inst = new LargeMultiply();
//Object creation of class LargeMultiply
String v = inst.multiply(inputA, inputB);
// calling method multiply()
System.out.println("RESULT:");
System.out.println(v); // Output result
}
/*-- multiply method , return type String ,
2 String parameters---- */
public String multiply(String num1, String num2) {
String[] str1 = num1.split("");
//Split the number string into array with each element
// containing single digit
String[] str2 = num2.split("");
int m1 = 0;
int m2 = 0;
String[] resultStringArray = new String[str2.length];
//Initializing the resultString array which has input
// string2 in Array form with each element
//in different Index
LargeMultiply inst1 = new LargeMultiply();
//multiply every element of String2 with the String1
// using multiplySmall() method
for (int j = str2.length - 1; j > 0; j--)
resultStringArray[j] = inst1.multiplySmall(num1, str2[j]);
// call to multiplySmall() method
for (int i = num2.length() - 1; i > 0; i--) {
for (int j = 1; j <= i; j++) {
resultStringArray[j] = resultStringArray[j].concat("0");
//concat number of zeroes which are equal to the number of
//digits in 2nd number
}
}
String finalResult = "0"; // Initialize output String
for (int j = str2.length - 1; j > 0; j--) {
finalResult = inst1.add(finalResult, resultStringArray[j]);
//call to adding large numbers method
}
return finalResult; //return result
}
/*----Method to multiply a single digit to large digits----- */
public String multiplySmall(String large, String small) {
int smallInt = Integer.parseInt(small);
//String to int conversion
String[] s = large.split("");
String[] largeStringArray = new String[s.length - 1];
//Initiate a String array
//as discussed earlier split function makes
//an array which has first element “” empty
//so in order to remove that element
//we have to make another array,here it is
//largeStringArray
for (int i = 0; i < largeStringArray.length; i++) {
largeStringArray[i] = s[i + 1];
}

int[] largeInt = new int[largeStringArray.length];
//make a integer array for num1 which is considered large
for (int i = 0; i < largeStringArray.length; i++) {
largeInt[i] = Integer.parseInt(largeStringArray[i]);
//String to int conversion of every element
}
//Logic for carry
int carry = 0;
String[] resultCarry = new String[2];
//carry number represented by 2 element String array
String result = "";// result to be stored in string form
int resultInt = 0;//carry in form of integer
//int count = 0;
for (int i = largeStringArray.length - 1; i >= 0; i--) {
resultInt = largeInt[i] * smallInt + carry;
//multiplying every element of the largeInt
// with single digit integer
carry = 0;//after every multiplication carry to be made zero
if (i == 0) { // if no carry
resultInt = resultInt; // no change
} else { // If carry then
if (resultInt / 10 > 0) {
//if output on multiplying is of 2 digits
String s2 = Integer.toString(resultInt);
//convert int to String
String[] s3 = s2.split("");
// Split the Result on basis of every digit
carry = Integer.parseInt(s3[1]);
//make the first digit stored on index 1, carry
resultInt = resultInt % 10;
//keep the unit place digit of the carry
//result which is concated later
}
}
result = Integer.toString(resultInt).concat(result);
//convert unit place of result to String and concat
// it with previous results
}
return result;
//return result, which is multiplication of single
// digit number of num2 to the whole num1
}
/*----------Method to add 2 large numbers-------- */
public String add(String num1, String num2) {
char[] tempChar1 = num1.toCharArray();
//converting String num1 to character Array using
// toCharArray() function
char[] tempChar2 = num2.toCharArray();
//converting String num2 to character Array
//using toCharArray() function
char[] ch2;
char[] ch1;
//condition to check which number is longer
if (tempChar1.length > tempChar2.length) { // if 1st is larger
ch2 = tempChar1; // assign ch2 to num1
ch1 = tempChar2; // assign ch1 to num2
} else { //if second is larger
ch2 = tempChar2;// assign ch2 to num2
ch1 = tempChar1;// assign ch1 to num1
} // so always the small number gets stored in ch1
String ans = ""; //Initiating Result String, ans
int tempAns = 0;
int carry = 0;
int smallerStringIndex;
char smallerStringNumber;
smallerStringIndex = ch1.length - 1;
//represents index for smaller number
for (int i = ch2.length - 1; i >= 0; i--) {
//if index becomes negative add '0'
// at the start of smaller digit, will have no effect
if (smallerStringIndex < 0) {
smallerStringNumber = '0';
} else {
smallerStringNumber = ch1[smallerStringIndex];
}
tempAns = (smallerStringNumber + ch2[i] - 96 + carry);
// -96 the ASCII value gives actual value of the char in int
if (tempAns >= 10) {
//if there is a carry
tempAns = tempAns - 10;
//tempAns now contains the unit digit after addition
ans = tempAns + ans;
carry = 1; //assign 1 to carry
} else {
ans = tempAns + ans; //case of no carry
carry = 0;
}
smallerStringIndex--; //reduce index by 1
}
if (carry == 1) {
ans = "1" + ans;
//adding String "1" to final result if at the
//end the last digits add up to give a carry
}
return ans; //return result in String
}
}

- Laxminarayana September 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1337 * 42

- Anonymous September 04, 2017 | 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