Samsung Interview Question for Developer Program Engineers


Country: United States
Interview Type: In-Person




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

We can write a solution using producer & consumer(Sharing a list of objects) and singleton pattern.

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

The code will look like this.But the problem is even after calling the returnObject the client class can keep on using the Object.I am still thinking on a fullproof code.

class TwentyOnly{
    private static List<TwentyOnly> twenty= new ArrayList(20);

    private static int counter=0;
    private TwentyOnly(){
    }
    public static synchronized TwentyOnly  borrow(){
      if(counter<20){
        twenty.add(0,new TwentyOnly());
        counter++;
        return twenty.get(0);
      }
      throw new RUntimeException("All Allowed instances allowed");
    }

    public static synchronized void returnObject(TwentyOnly t){
         twenty.remove(t);
         counter--;
    }
}

- onlinesoumitra April 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you might want to do sth like this.

public static synchronized void returnObject(TwentyOnly t){
         twenty.remove(t);
         counter--;
	  t=null;
    }

- Prithvi April 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I made some change based on onlinesoumitra code

class TwentyOnly{
    private static List<TwentyOnly> twenty= new ArrayList(20);

    private static int counter=0;
    private TwentyOnly(){
    }
    public  synchronized void  borrowObject(){
      if(counter<=20){
        twenty.add(0,new TwentyOnly());
        counter++;
        //return twenty.get(0);
      }else
      {
    	  try {
			this.wait();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
      }
      this.notify();
      //throw new RuntimeException("All Allowed instances allowed");
    }

    public  synchronized void returnObject(TwentyOnly t){
    	if(counter>0){
         twenty.remove(t);
         counter--;
    	}else
    	{
    		try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    	this.notify();
    }
}

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

public class TwentyObjectPool{
       
      private static class ExpensiveObject implements FancyInterface{
               private boolean dirty = false;
             
               private void sayHello(){
                      if(!dirty){
                      	 System.out.println("Hello");
                      }else{
                      	throw new IllegalStateException("I am dirty");
                      }
               }     
      } 
      
      private List<ExpensiveObject> objects = Arrays.asList(new ExpensiveObject(), new ExpensiveObject() //18 more times);
      private int count = 20;

      public FancyInterface borrowObject(){
	 if(count => 0){
	 	objects.remove(--count);
	 }else{
	 	throw new IllegalStateException("Out of Objects");
	 }
      }
      
      public void returnObject(FancyObject obj){
      	assert obj instanceof ExpensiveObject;
      	ExpensiveObject expObj = (ExpensiveObject)obj;
      	expObj.dirty = true;
      	objects.add(new ExpensiveObject());
      	count++;
      }
      
}

- gauarv J June 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.java.basic.datastructure;

public interface ObjectPool <E>{
public static final int NUMBER_OF_OBJECT_CREATED=20;
public E borrowObject() throws InterruptedException ;
public void returnObject(E object) throws InterruptedException ;
/**
* Get the number of Live Objects(Objects currently in use by other classes).
* @return
*/
public int aliveObjectCount();


}


package com.java.basic.datastructure;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class TwentyOnly implements ObjectPool<TwentyOnly>{
private static BlockingQueue<TwentyOnly>queue;
static{
queue=new ArrayBlockingQueue<TwentyOnly>(20);
for(int index=0;index<NUMBER_OF_OBJECT_CREATED;index++) {
queue.add(new TwentyOnly());
}
}

@Override
public TwentyOnly borrowObject() throws InterruptedException {
return queue.take();
}

@Override
public void returnObject(TwentyOnly object) throws InterruptedException {
queue.put(object);

}

@Override
public int aliveObjectCount() {
// TODO Auto-generated method stub
return (NUMBER_OF_OBJECT_CREATED-queue.size());
}




}

- VIKASH KUMAR SINHA September 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

package com.java.basic.datastructure;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class TwentyOnly implements ObjectPool<TwentyOnly>{
private static BlockingQueue<TwentyOnly>queue;

private TwentyOnly(){

}
static{
queue=new ArrayBlockingQueue<TwentyOnly>(20);
for(int index=0;index<NUMBER_OF_OBJECT_CREATED;index++) {
queue.add(new TwentyOnly());
}
}

@Override
public TwentyOnly borrowObject() throws InterruptedException {
return queue.take();
}

@Override
public void returnObject(TwentyOnly object) throws InterruptedException {
queue.put(object);

}

@Override
public int aliveObjectCount() {
// TODO Auto-generated method stub
return (NUMBER_OF_OBJECT_CREATED-queue.size());
}




}

- VIKASH KUMAR SINHA September 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

package com.java.basic.datastructure;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class TwentyOnly {

/**
* BlockingQueue implementations are thread-safe. All
* queuing methods achieve their effects atomically using internal
* locks or other forms of concurrency control
* BlockingQueue can safely be used with multiple
* producers and multiple consumers.
*/
private static BlockingQueue<TwentyOnly>queue;
private static int NUMBER_OF_OBJECT_CREATED=20;

/**
* Constructor must be private
*/
private TwentyOnly(){}

static{
queue=new ArrayBlockingQueue<TwentyOnly>(20);
for(int index=0;index<NUMBER_OF_OBJECT_CREATED;index++) {
queue.add(new TwentyOnly());
}
}

public static TwentyOnly borrowObject() throws InterruptedException {
return queue.take();
}

public static void returnObject(TwentyOnly object) throws InterruptedException {
queue.put(object);

}

public static int aliveObjectCount() {
return (NUMBER_OF_OBJECT_CREATED-queue.size());
}




}

- VIKASH KUAMR SINHA September 25, 2013 | Flag


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