Interview Question for Software Engineer / Developers


Country: India




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

with no other information given in the question, I can only think of the following.

public interface Walker{
	public void walk();	
	public void breath();
}

public interface Talker{
	public void talk(); 
	public void breath() ;
}

public class Swimmer implements Walker, Talker{
	public void walk() {
		System.out.println("I walk");
	}
	
	public void talk() {
		System.out.println("I talk");
	}
	
	public void swim() {
		System.out.println("I swim");
	}
	
	public void breath() {
		System.out.println("I breath");
	}
}

- subahjit July 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
3
of 3 votes

I think there should be 4 interfaces (Breather, Talker, Walker, Swimmer) in total and 3 classes (Talker, Walker, Swimmer)

- Anonymous July 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think they're just asking for the inheritance model
but the question also implies a Breather base class.

Breather
	Walker is a Breather
	Talker is a Breather
		Swimmer is a Walker and a Talker

- Larry July 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think its a diamond problem.
Breathe
/ \
Walk Talk
\ /
Swim

Breathe will have its own method so it must be abstract class.
And so on for walk and talk. So, question may be intended to check how you will resolve diamond problem for dynamic polymorphisam.

- Anonymous July 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Open ended kind of questions allowing for further discussion and interpretation. For example, do they walk, breath, swim, talk in the same way? What happens when adding a new type of person like Sleeper who can sleep, walk and breath?

interface Breathable {
		void breath();
	}
	interface Walkable {
		void walk();
	}
	interface Talkable {
		void talk();
	}
	interface Swimmable {
		void swim();
	}

	ModerateBreathing implement Breadable {
		void breath() {
			// 
		}
	}

	HeavyBreathing implements Breathable {
		void breath() {
			//
		}
	}

	class Walker : implements Walkable, Breathable {
		// Delegates
		Walkable w;
		Breathable b;
		Walker(Walkable w, Breathable b) {
			this.w= w;
			thisb = b;
		}
		void walk() {
			w.walk();
		}
		void breath() {
			b.breath();
		}
	}

	class Swimmer : implements Swimmable, Talkable, Breathable, Walkable {
		Swimmable s;
		Talkable t;
		Breathable b;
		Walkable w;
		Swimmer(Swimmable s, Talkable t, Breathable b, Walkable w) {
			// Save delegates
			...
		}

		// Implements the interface using deletegates
		...
	}

	public static void main(..) {
		Walker w1= new Walker(new SlowWalking(), new ModerateBreathing());
		Walker w2= new Walker(new FastWalking(), new ModerateBreathing());
		Swimmer s1 = new Swimmer(new FreeStyleSwimming(), new FastTalking(), HeavyBreathing(), new SlowWalking());
	}

- Anonymous September 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is a Strategy pattern problem. Walk and Swim behaviors implemented similar to Talk behavior. One of the advantages of this approach is that we can add or modify behaviors on fly (see Simulator class).

public abstract class Person
{
WalkBehavior walkBehavior;
TalkBehavior talkBehavior;
SwimBehavior swimBehavior;

public abstract void display();
	
public void breath()
{
	System.out.println("I can breath.");
}

public void performTalk()
{
	talkBehavior.talk();
}

public void performWalk()
{
	walkBehavior.walk();
}

public void performSwim()
{
	swimBehavior.swim();
}

public void setWalkBehavior(WalkBehavior walkBehavior)
{
	this.walkBehavior = walkBehavior;
}

public void setTalkBehavior(TalkBehavior talkBehavior)
{
	this.talkBehavior = talkBehavior;
}

public void setSwimBehavior(SwimBehavior swimBehavior)
{
	this.swimBehavior = swimBehavior;
}
}

public interface TalkBehavior
{
public void talk();
}

public class Talk implements TalkBehavior
{
public void talk()
{
	System.out.println("I can talk");
}
}

public class TalkNoWay implements TalkBehavior
{
public void talk()
{
	System.out.println("I can't talk");
}
}

public class Talker extends Person
{
Talker()
{
	walkBehavior = new WalkNoWay();
	talkBehavior = new Talk();
	swimBehavior = new SwimNoWay();
}

public void display()
{
	System.out.println("I'm a talker");
}
}

public class Walker extends Person
{
Walker()
{
	walkBehavior = new Walk();
	talkBehavior = new TalkNoWay();
	swimBehavior = new SwimNoWay();
}

public void display()
{
	System.out.println("I'm a walker");
}
}

public class Swimmer extends Person
{
Swimmer()
{
	walkBehavior = new Walk();
	talkBehavior = new Talk();
	swimBehavior = new Swim();
}

public void display()
{
	System.out.println("I'm a swimmer");
}
}

public class Simulator
{
public static void main(String[] args)
{
	//Talker -> Talk and Breath 
	Person talker = new Talker();
	talker.display();
	talker.performTalk();
	talker.performWalk();
	talker.performSwim();
	talker.breath();
	System.out.println();
	
	//Walker -> Walk and Breath 
	Person walker = new Walker();
	walker.display();
	walker.performTalk();
	walker.performWalk();
	walker.performSwim();
	walker.breath();
	System.out.println();
	
	// Swimmer -> Swim, Talk, Walk and Breath.
	Person swimmer = new Swimmer();
	swimmer.display();
	swimmer.performTalk();
	swimmer.performWalk();
	swimmer.performSwim();
	swimmer.breath();
	System.out.println();
	
	// add talk behavior to Walker
	walker.setTalkBehavior(new Talk());
	walker.display();
	walker.performTalk();
	walker.performWalk();
	walker.performSwim();
	walker.breath();
	System.out.println();
	
}
}

- sveta September 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can you guys review it
interface breath
{
void canbreath();

}
interface Walk
{
void canwalk();

}
interface Swim
{
void canwalk();

}
interface Talk
{
void Talk();
}

class Breather implements breath
{

@Override
public void canbreath() {
// TODO Auto-generated method stub

}

}

class Talker extends Breather implements Talk
{
@Override
public void Talk() {
// TODO Auto-generated method stub

}

}
class Walker extends Breather implements Walk
{

@Override
public void canwalk() {
// TODO Auto-generated method stub

}


}

public class Swimmer extends Walker implements Talk,Swim{

public void swim() {
System.out.println("I swim");
}

@Override
public void Talk() {
// TODO Auto-generated method stub

}


}
//Breather is common for all classes so need not to have any particular class structure to call
//write a factory method to write an update or get any update.....

- Sverky December 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Are you a c++ developer? Diamond inheritance can be resolved with virtual inheritance.

class Breather
{
	virtual Breath();
}
class Walker: public virtual Breather
{
	virtual Walk();
};


class Talker: public virtual Breather
{
	virtual Talk();
};


class Swimmer: public Walker, Talker
{
	virtual Swim();
};

- weihuaw February 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Is this valid?

Incomplete -> Question, Typical

- Anonymous July 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.


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