## ThoughtWorks Interview Question for Developer Program Engineers

Country: India
Interview Type: In-Person

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

``````int divide(int divisor, int dividend) {

int quotient = 0;
while (divisor <= dividend) {
dividend = dividend - divisor;
quotient++;
}
return quotient;
}``````

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

I guess the condition is wrong it should be

``while (divisor < dividend)``

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

divisor <= dividend --- this is the correct condition. try (8,4).

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

``new BigDecimal(String.valueOf(n)).divide(new BigDecimal(String.valueOf(m)))``

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

How about we run the loop while(dividend != 0)

Because at some point, it will be 0, provided the dividend is completely divisible by the divisor.

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

public double Divide(double numerator, double denominator)
{
double temp = 1.0;

if (numerator == 0)
return 0;

if (numerator < denominator)
{
numerator *= 10;
temp = 0.1 * Divide(numerator, denominator);
numerator = denominator;
}

return temp + Divide(numerator - denominator, denominator);
}

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

Considering
1. negative numbers in both numerator and denominator
2. divide by zero

public double Divide(double numerator, double denominator)
{
bool negativeResult = false;
double temp = 1.0;

if (denominator == 0)
{
throw new Exception("Divide by zero error");
}
else if (numerator == 0)
{
return 0;
}
else if (numerator < 0 && denominator < 0)
{
numerator *= -1;
denominator *= -1;
}
else if (numerator < 0 && denominator >= 1)
{
numerator *= -1;
negativeResult = true;
}
else if (numerator >= 1 && denominator < 0)
{
denominator *= -1;
negativeResult = true;
}

if (numerator < denominator)
{
numerator *= 10;
temp = 0.1 * Divide(numerator, denominator);
numerator = denominator;
}

return (negativeResult ? -1 : 1) * (temp + Divide(numerator - denominator, denominator));
}

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

static int limit = 0;
double Divide(double numerator, double denominator)
{
double temp = 1.0;

if (numerator == 0 || limit > 10)
return 0;

if (numerator < denominator)
{
++limit;
numerator *= 10;
temp = 0.1 * Divide(numerator, denominator);
numerator = denominator;
}

return temp + Divide(numerator - denominator, denominator);
}

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

in simple mathematics, multiplication is considered as advanced form of addition, and division is considered as advanced for of subtraction. In this case, I hope that though we're not supposed to use / or %, we can get away by using -

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

``````int sign(int a){
return a<0;
}

int div(int a, int b){
int A = abs(a);
int B = abs(b);

int counter =0;
while((counter+1)*b<=a){
counter++;
}

// one is negative
if(sign(a)+sign(b)==1){
counter =-counter;
}

return counter;
}``````

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

For this I went back to how we used to do multiplication and division by hand on paper, and implemented those algorithms... but in base2 using bit operations. Also special-cased zeros and negative numbers:

``````private static int multiply(int n1, int n2) {
if (n1 == 0 || n2 == 0) return 0;
if (n1 < 0 && n2 < 0) return multiply(-n1, -n2);
if (n1 < 0) return -1 * multiply(-n1, n2);
if (n2 < 0) return -1 * multiply(n1, -n2);

int result = 0;
int currentShift = 0;
while ((n2 >> currentShift) > 0) {
if ((1 & (n2 >> currentShift)) == 1) {
result += n1 << currentShift;
}
currentShift++;
}
return result;
}

private static int divide(int divisor, int dividend) {
if (divisor == 0) throw new IllegalArgumentException("Division by Zero");
if (divisor < 0 && dividend >= 0) return -1 * divide(-divisor, dividend);
if (divisor > 0 && dividend < 0) return -1 * divide(divisor, -dividend);
if (divisor > dividend) return 0;

long div = divisor;
int currentShift = 0;
while ((div << currentShift) < dividend) {
currentShift++;
}
currentShift--;
int result = 0;
for (; currentShift >= 0; currentShift--) {
if ((div << currentShift) <= dividend) {
result |= (1 << currentShift);
if ((div << currentShift) < dividend) {
dividend -= (div << currentShift);
} else {
break;
}
}
}
return result;
}``````

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

``````import java.util.Scanner;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author suryakarthik.v
*/
public class Division {
static  int count=1;
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int count=1;
System.out.println("give the dividend");
double m=sc.nextDouble();
System.out.println("Give the divisor");
double n=sc.nextDouble();
System.out.println("The quotient is "+quotient(m,n));
System.out.println("The remainder is "+rem(m,n));
;
}

public static int quotient(double m,double n){

double N=n;
if(m<n)return 0;
else if(m==n)return 1;
else{
for (;n<m-N; n=n+N) {
count++;
}
}
return count;
}
public static double rem(double m,double n){
return m-n*count;
}
}``````

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

void divide(int divisor,int divident)
{
int quotient=0,remainder,flag;
if(divident!=0)
{
if(divisor<0 && divident<0)
flag=1;
if(divisor<0 && divident>0)
flag=2;
if(divisor>0 && divident<0)
flag=3;
else
flag=4;

if(divisor<0)
divisor=-divisor;
if(divident<0)
divident=-divident;

while(divisor<0)
{
divisor-=divident;
quotient++;
}
remainder=divisor+divident;
if(flag==1)
remainder=-remainder;
if(flag==2)
{remainder=-remainder; quotient = -quotient;}
if(flag==3)
quotient=-quotient;

cout<<"Quotient: "<<quotient;
cout<<" Remainder: "<<remainder;
}

}

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

static int divide1(int divisor, int dividend)
{
int temp=1;
int retVal = 0;
for (int i = 0; !(temp == dividend || temp + divisor >= dividend); i++)
{
temp = divisor * i;
retVal = i;
}
return retVal;
}

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

for (int i = 1, result = 0, a = 24, b = 3; i <= a; i += b) ++result;

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

int q=0;
while (a<b)
{b=b-a};
printf(q,b)

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

int q=0;
while (a<b)
{b=b-a;
q=q+1};
printf(q,b);

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

``````import java.util.*;

class Divide{
int num;
int den;
double ans;
static double result=1;

public static void main(String args[])
{
Divide obj=new Divide();
Formatter fm=new Formatter();
Scanner sc=new Scanner(System.in);
System.out.println("enter num");
obj.num=sc.nextInt();
System.out.println("enter den");
obj.den=sc.nextInt();
obj.ans=1;
if(obj.num>obj.den)
{
while(obj.num>obj.den)
{
obj.num=obj.num-obj.den;
obj.ans++;

}
}
else
{
while(obj.num<obj.den)
{
obj.num=obj.num*10;
result=result/10;

}
while(obj.num>obj.den)
{
obj.num=obj.num-obj.den;
obj.ans++;

}
}
if(result>=1)
{
}
else
{
}
}
}``````

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

package com.java;

import java.math.*;

public class Divide {

public static void main(String[] Args){

int divident=0,divisor=0,res=1,i=1;
boolean flag=false;

//if(Integer.parseInt(Args[0])>Integer.parseInt(Args[1])){

divident=Integer.parseInt(Args[0]);
divisor=Integer.parseInt(Args[1]);
//}
//else{
// divident=Integer.parseInt(Args[1]);
// divisor=Integer.parseInt(Args[0]);
//}
if(divident<0 ){

divident=0-divident;
flag=true;
}
if(divisor<0){
divident=0-divident;
flag=true;
}
if(( divident<0 && divisor<0)){

divident=0-divident;
divident=0-divident;
}

res=divident-divisor;
while(true){

if(divisor>0){

//res=divident-(divisor*i);
//i++;

if(res==0 || res==1){
if(flag)
i=-i;

System.out.println("divide// "+i);
break;
}
else if(res <0){

i--;
if(flag)
i=-i;

System.out.println("divide// "+ (i));
break;

}
res=res-divisor;
i++;
}
else{

System.out.println(divisor);
break;
}
}
System.out.println("Modulo%% "+res);

}

}

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

``````public int divide(int numerator, int denominator) {
int value = 0;
int sign = 1;
if (numerator == 0) {
return value;
}
if (denominator == 0) {
System.out.println("Infinity");
return -1;
}
if (numerator < 0 || denominator < 0) {
sign = -1;
if (numerator < 0) {
numerator *= -1;
}
if (denominator < 0) {
denominator *= -1;
}
}

while (numerator >= denominator) {
numerator -= denominator;
value++;
}
return value * sign;
}``````

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

public class DivisionDemo {
public static void main(String args[]){
int a=100;
int b=5;
int c=0;
int d=b;
while(a>=d){
d+=b;
c++;
}
System.out.println(c);
}

}

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

``````public static void divide(int dividend, int divisor) {
int quotient = 0;
for(int tempDividend = 0;tempDividend<=divisor;tempDividend++) {

if (tempDividend == divisor) {
quotient++;
tempDividend=0;
dividend = dividend - divisor;
}
if(dividend<divisor) {
System.out.println("remainder:"+dividend);
break;
}
}
System.out.println("quotient:"+quotient);``````

}

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

``````private static double Divide(int numerator, int denominator)
{
double result = 0;

if (numerator != 0 && numerator < denominator)
{
result = .1 * Divide(numerator * 10, denominator);
}

if (numerator - denominator >= denominator || numerator - denominator >= 0)
{
numerator -= denominator;
result = Divide(numerator, denominator);

result++;
}

return result;
}``````

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

public class demo {
public static void main(String args[]){
int x=6,y=10;

System.out.println(x+y>>1);
}
}

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

``````public class demo {
public static void main(String args[]){
int x=6,y=10;

System.out.println(x+y>>1);
}
}``````

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

public static double divide(int numerator, int denominator)
{
int res = 0, q = 0;
if(numerator<denominator)
{
numerator = numerator * 10;
return 0.1 * divide(numerator, denominator);
}
while (numerator >= denominator)
{
numerator = numerator - denominator;
res++;
}
if (numerator > 0)
q = numerator;

return res;
}

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

``````#include <iostream>
using namespace std;

int main(){
// a is the numerator, b is the denominator
int a, b;
int count = 0;
cin>>a>>b;
while(a != 0){
a = a - b;
count++;
}
cout<<count;
return 0;
}``````

Can anyone suggest what can be done for the case when the numerator is not completely divisible by the denominator. Also, what about division of floating point numbers? Which basically means, what if the answer is a floating point number?

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

in Java -

``````public class DivisionWithoutOperator {

public static void main(String[] args) {

int x1 = 13, x2 = 5;

int divisor = (x1 > x2) ? x2 : x1;
int dividend = (x1 > x2) ? x1 : x2;
int multiplier = 1, quotent;
while (true) {
if ((divisor * multiplier) > dividend) {
quotent = multiplier - 1;
break;
} else
multiplier++;

}

System.err.println("Division result = " + quotent);
}

}``````

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

public class DivisionWithoutOperator {

public static void main(String[] args) {

int x1 = 13, x2 = 5;

int divisor = (x1 > x2) ? x2 : x1;
int dividend = (x1 > x2) ? x1 : x2;
int multiplier = 1, quotent;
while (true) {
if ((divisor * multiplier) > dividend) {
quotent = multiplier - 1;
break;
} else
multiplier++;
}

System.out.println("Division result = " + quotent);
}

}

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.