Qualcomm Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

In a singleton pattern, the class always has a reference (static) to the object. Extend this to 5 object references and you have a Fiveton. I presume that the class using the singleton object must know which object to use.

A clarification: So when an object is GCed, you decrement count, create new object but not increment count again??

In this pattern, the object does not get GCed unless you explicitly release the reference and create a new object simultaneously. Additionally if the object is getting GCed and constructed, then make the references 'volatile' to prevent any concurrency issues.

- axecapone July 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If you really need that kind of dark bizarre voodoo behavior, you can use synchronization when creating the objects and a finalizer to decrement the object count when an object gets GC'd. But since GC is not guaranteed to run at any particular time, doing this kind of stuff is usually a worse idea than it seems.

- eugene.yarovoi July 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

coderanch.com.t.494880.java.java.Fiveton-just-like-Singleton-maximum

jeet-java.blogspot.in.2010.10.fiveton-class.html

- mac July 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
import java.util.List;


public class FivetonImpl {

	public static void main(String[] args) {
		System.out.println(Fiveton.getInstance());
		System.gc();
		System.out.println(Fiveton.getInstance());
		System.out.println(Fiveton.getInstance());
		System.gc();
		System.out.println(Fiveton.getInstance());
		System.out.println(Fiveton.getInstance());
		System.out.println(Fiveton.getInstance());
		System.gc();
		System.out.println(Fiveton.getInstance());
		System.out.println(Fiveton.getInstance());
		System.out.println(Fiveton.getInstance());
		System.gc();
		System.out.println(Fiveton.getInstance());
		System.out.println(Fiveton.getInstance());
		System.out.println(Fiveton.getInstance());
		System.gc();
		System.out.println(Fiveton.getInstance());
		System.out.println(Fiveton.getInstance());
		System.gc();
		System.out.println(Fiveton.getInstance());
	}
	
}

class Fiveton
{
	int value;
	static List<Fiveton> freeFives = new ArrayList<Fiveton>(5);
	static {
		freeFives.add(new Fiveton());
		freeFives.add(new Fiveton());
		freeFives.add(new Fiveton());
		freeFives.add(new Fiveton());
		freeFives.add(new Fiveton());
	}
	
	public static synchronized Fiveton getInstance()
	{		
			if(freeFives.size() > 0)
			{
				return freeFives.remove(freeFives.size() - 1);				
			}
		 
		return null;
	}
	
	@Override
	protected void finalize() throws Throwable {	
		super.finalize();
		System.out.println("finalize called");
		freeFives.add(new Fiveton());
	}
	
}

- sudarsan.dabburi August 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

s

- s July 18, 2014 | 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