Expedia Interview Question for Software Engineer / Developers


Country: United States




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

The Singleton is a object-oriented designed pattern where you ensure there is one and only one instance of a class being used. An example where you want to use this pattern is when designing cash register. A cash register can be decompose to smaller individual items including a sales tax calculator. There should be only one instance of the tax calculator used.

- pete May 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public class Singleton {

private static Singleton singleton =null;


private Singleton(){

}


public static Singleton getInstance(){

if(singleton = =null){


singleton = new Singleton();

}
return singleton;

}



}

- naveengarg2k5 July 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

getInstance method should be synchronized

- Hari August 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

hi Hari you are right but i right for general purpose but we also prevent from cloning so will use clone() method

protected Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException("Cloning of this class is not allowed");
}

- naveengarg2k5 August 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Cloning has nothing to do with synchronization. If two threads read from a field at the same time and make two different alterations to it and write it back, you are going to lose one of the alterations.

- maverick September 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I know Synchronization and cloning both are different things but in case of Cloning than we will have make cloning restriction.

- naveen September 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ maverick
its not about loosing one alterations. if two threads call gentInstance at roughly the same time and if thread1 gets preempted after evaluating if cond to true and thread2 completes getInstance call and it creates one instance and then thread1 wakes up and goes on to create one more instance

- Anonymous September 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

creating only one object for a particular class.program to create a single ton design pattern
import java.io.*;
class SingleTon
{
static SingleTon s1=new SingleTon();
private SingleTon(){
}
static SingleTon getObject()
{
return s1;
}
public static void main(String... args) throws Exception
{
SingleTon st=SingleTon.getObject();
SingleTon st1=SingleTon.getObject();
if(st.equals(st1))
System.out.println("equal");
}
}

- balaji May 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What is the scope of a singleton object? is that "an object per jvm"?

- Amit April 18, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

anyone played with java enums to create singleton? / or used serialization or reflection API to break it?? any comments on this?

- jimmy514in March 17, 2013 | 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