Epic Systems Interview Question
SDE1sCountry: United States
Interview Type: Written Test
Hi, your code seems to work only for numbers that differ by 1 in length. However I think it should't be like that. As per my understanding of code we need to print the two halves that sum up as less than or equal to the other number. Here is my implementation.
public static void splits(String number1, String number2) {
int num = Integer.parseInt(number2);
String half1, half2;
for (int index = 1; index < number1.length(); index++) {
half1 = number1.substring(0, index);
half2 = number1.substring(index);
if ((Integer.parseInt(half1) + Integer.parseInt(half2)) <= num) {
// could be easily changed here to track the halves that sum
// closest to 2nd Number
System.out.println(half1 + "--" + half2);
}
}
}
public class twoHalsves {
public static void main(String[] args)
{
int first = 31970;
int sec = 1000;
int div = first;
int quotent = 0;
int multiplier = 0;
int sum = 0;
int min = Integer.MAX_VALUE;
int part1 = 0, part2 = 0;
while(div!=0)
{
quotent = (int) (quotent + (div%10) * Math.pow(10, multiplier));
multiplier++;
div = (int)div/10;
sum = quotent+div;
if(sum<=sec)
{
int diff = sec - sum;
if(diff<min)
{
min = diff;
part1 = quotent;
part2 = div;
}
}
}
System.out.println("Parts:"+ part1+" "+part2);
}
}
int main()
{
int first = 19967;
const int second = 1000;
int h1,h2=0;
int divi = first;
int divs = 10;
int count = 1;
int mindiff =second;
while((first%divs) != first)
{
int c = (first%divs) ;
int d = (first-c)/divs;
int sum = c+d;
int diff = sum > second ? sum - second : second - sum;
if (diff < mindiff)
{
h1 =c;
h2 =d;
}
cout << "c=" << c << " d=" << d << " diff=" << diff << " divs=" << divs << endl;
divs = divs*10;
}
cout << first <<second << endl;
cout << h1 << "," <<h2;
return 0;
}
int main()
{
int first = 19967;
const int second = 1000;
int h1,h2=0;
int divi = first;
int divs = 10;
int count = 1;
int mindiff =second;
while((first%divs) != first)
{
int c = (first%divs) ;
int d = (first-c)/divs;
int sum = c+d;
int diff = sum > second ? sum - second : second - sum;
if (diff < mindiff)
{
h1 =c;
h2 =d;
}
cout << "c=" << c << " d=" << d << " diff=" << diff << " divs=" << divs << endl;
divs = divs*10;
}
cout << first <<second << endl;
cout << h1 << "," <<h2;
return 0;
}
int main()
{
int first = 19967;
const int second = 1000;
int h1,h2=0;
int divi = first;
int divs = 10;
int count = 1;
int mindiff =second;
while((first%divs) != first)
{
int c = (first%divs) ;
int d = (first-c)/divs;
int sum = c+d;
int diff = sum > second ? sum - second : second - sum;
if (diff < mindiff)
{
h1 =c;
h2 =d;
}
cout << "c=" << c << " d=" << d << " diff=" << diff << " divs=" << divs << endl;
divs = divs*10;
}
cout << first <<second << endl;
cout << h1 << "," <<h2;
return 0;
}
- Vir Pratap Uttam April 17, 2015