Bloomberg LP Interview Question for Software Engineer Interns


Country: United States
Interview Type: Phone Interview




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

public static class Singleton{
  private Singleton instance;

  private Singleton(){
  }

  public static Singleton getInstance(){
    if (instance == null){
      instance = new Singleton();
    }
    return instance;
  }
}

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

Remove "static" from the first line, it was a typo. It should be:

public class Singleton

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

instance variable should be static

package career.cup.bloomberg;

public class Singleton {
public static Singleton instance = null;

private Singleton() {

}
public static Singleton getInstance(){
if(instance == null) {
instance = new Singleton();
}

return instance;
}

}

- hm January 11, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

First, your instance variable should be static
Second, this is not thread safe. use double checked locking.

- sam March 09, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

As name suggest Singleton create only one object. it is more useful in such situation like when you have only 1 DB license & you want to allow only one connection object of the DB.

Refer below link becoz it has different types of singleton examples

journaldev.com/1377/java-singleton-design-pattern-best-practices-with-examples

- Hitesh Singh December 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

At that link *DO NOT* use the double checked linked approach. That one could fail to properly synchronize.

The best approach is the "Bill Pugh" approach.

- zortlord December 19, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Add "synchronized" keyword to make the code thread-safe.

public class Singleton{

	private Singleton instance;
	
	private Singleton(){}

	public static synchronized getInstance(){  // Add synchronized keyword to make it thread-safe
		if(instance == null)
			return new Singleton();
		else 
			return instance;
	}
}

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

public static synchronized Singleton getInstance()

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

public class SingletonExample {


private static SingletonExample single;

private SingletonExample() {

}
static {
if (single == null) {

try {
single = new SingletonExample();
} catch(Exception e){
e.printStackTrace();
}
}
}

public static SingletonExample getInstance() {
return single;
}

}
class test {
public static void main(String args[]) {
SingletonExample abc = SingletonExample.getInstance();
}
}

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

One of the Better way of writing a Singleton is using the Static Inner Helper Class (Bill Pugh Method).

public class Singleton {
 
    private Singleton(){}
     
    private static class SingletonHelper{
        private static final Singleton m_instance = new Singleton();
    }
     
    public static Singleton getInstance(){
        return SingletonHelper.m_instance;
    }
}

When the singleton class is loaded, SingletonHelper class is not loaded into memory and only when someone calls the getInstance method, this class gets loaded and creates the Singleton class instance.
This is the most widely used approach for Singleton class as it doesn’t require synchronization.

- R@M3$H.N December 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Test {
private Test _instance = null;
private Test(){

}
public Test getInstance(){
if(_instance == null)
return new Test();
return _instance;
}
}

Singleton classes lets you have only once instance of the class. For example, in android application development the same activity class has to be used for user interaction thus a singleton class is used.

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

public class Test {
private Test _instance = null;
private Test(){

}
public Test getInstance(){
if(_instance == null)
return new Test();
return _instance;
}
}

Singleton classes lets you have only once instance of the class. For example, in android application development the same activity class has to be used for user interaction thus a singleton class is used.

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

please review my code and let me know if any enhancement is required

package com.rk.core.java;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamException;
import java.io.Serializable;

public class PureSingleton implements Serializable, Cloneable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1526499224432020993L;
	private static PureSingleton instance = null;
	private static Object mutex = new Object();

	private PureSingleton() {
	}

	public static PureSingleton getInstance() {
		if (instance == null) {
			synchronized (mutex) {
				if (instance == null)
					instance = new PureSingleton();
			}
		}
		return instance;
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		throw new CloneNotSupportedException("PureSingleton cannot be cloned");
	}

	private Object readResolve() throws ObjectStreamException {
		return getInstance();

	}

	public static void main(String[] args) throws FileNotFoundException,
			IOException, ClassNotFoundException {
		PureSingleton instanceOne = PureSingleton.getInstance();
		ObjectOutput out = new ObjectOutputStream(new FileOutputStream(
				"filename.ser"));
		out.writeObject(instanceOne);
		out.close();

		// deserailize from file to object
		ObjectInput in = new ObjectInputStream(new FileInputStream(
				"filename.ser"));
		PureSingleton instanceTwo = (PureSingleton) in.readObject();
		in.close();

		System.out.println("instanceOne hashCode=" + instanceOne.hashCode());
		System.out.println("instanceTwo hashCode=" + instanceTwo.hashCode());

		try {
			PureSingleton instanceThree = (PureSingleton) instanceOne.clone();
			System.out.println("instanceThree hashCode="
					+ instanceThree.hashCode());
		} catch (CloneNotSupportedException e) {
			System.out.println(e.getMessage());
		}

	}
}

- ravikoda.usa January 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Double checked locking is broken in java 1.4 and before. So you have to use volatile keyword for the instance variable and that too only in java 1.5+

- sam March 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Reason why double checked linked approach is avoided?

- skum January 12, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long:

ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;
If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a default serialVersionUID value for that class based on various aspects of the class, as described in the Java(TM) Object Serialization Specification. However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization. Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value. It is also strongly advised that explicit serialVersionUID declarations use the private modifier where possible, since such declarations apply only to the immediately declaring class--serialVersionUID fields are not useful as inherited members.

- ruppu May 28, 2017 | 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