CrimsonLogic Interview Question for Dev Leads Dev Leads


Country: India
Interview Type: In-Person




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

If programming language is java, then you can make use of generics.
The input variable should be <? extends Number> instead of just int or double.

Number in java is superclass of classes BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, and Short.

- Anonymous November 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

1. Method overloading.
Having an abstract overloaded method sumBill which accepts double parameter types and let derived classes override the method. It causes less flexibility to the developers as this technique will require the changes in every derived class.

2. Encapsulation
Box the primitive type integer or double in a user defined object and the object self sufficient to know the underlying the primitive data type.

class A{
sumBill(summableObject s1, summableObject s2);
}

3. Using Java.lang.Number class, you could pass any Integer, Double etc objects..
class A{
sumBill(Number s1, Number s2);
}
Please note that Number is not a primitive.

- shankar2869 November 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Have sumBill accept either an interface, a class or something that exposes methods that allows 1 element to compare to the other. Example

sumBill(SummableItem a, SummableItem b)

where

interface SummableItem
{
SummableItem Sum(SummableItem other);
}

- Roni November 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This doesnt make sense. Can someon explain this to me?

- Ra December 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I do not get this. Can you please elaborate the code.

- Anonymous February 14, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In c++ we can use function template

- sv November 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This can resolved by method Type Conversion.

public abstract class TestAbs {

public abstract void sum(double a,double b);
}public class AbsTesting extends TestAbs{


public static void main(String[] args) {
// TODO Auto-generated method stub

AbsTesting absTesting = new AbsTesting();
absTesting.sum(10.0, 20.0);
absTesting.sum(10, 20);
absTesting.sum(10L, 20L);
absTesting.sum(10f, 20f);
}

public void sum(double a,double b){
System.out.println("Sum of a + b :" +(a+ b) );
}
}

reuslt:
Sum of a + b :30.0
Sum of a + b :30.0
Sum of a + b :30.0
Sum of a + b :30.0

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

I guess the above solution will work,

the below code is also in the same lines


public Abstract Class Absbase<T>
{

public abtract <T> sum (T a,T b);

}

public class AbsImplementor: AbsBase
{
T _value;
public override sum <T> sum (T a,T b)
{

this._Value=a+b;
}

}

Public class Client Class
{

pubic static void main(strn[] args)
{
AbsImplementor absSum=new AbsImplementor()
System.out.println("Sum of a + b :" + absSum.sum(10,20) );
System.out.println("Sum of a + b :" + absSum.sum(10.44,20.55) );

}
}

- venkat.kundavaram December 21, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

class A{
public <T> T sum(T t1,T t2){
}
}

- Anonymous November 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the answer to this question will be using a visitor pattern.

- sarangkunte1991 December 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why not just add a method with sumBill(double, double) signature, separate from sumBill(int, int)?

- Anonymous December 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I vote to Roni's answer above.

sumBill(SummableItem a, SummableItem b)

where

interface SummableItem
{
SummableItem Sum(SummableItem other);
}

- pk January 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Technically...both summable object and generics solution can be used to avoid the situation of changing parameter types.

I wonder if there is a specific design pattern name for the solution. The only thing I can say is 'code to interface' rather than to concrete principle is applicable here.

- PK February 14, 2015 | 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