Interview Question


Country: United States




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

Decorator Pattern

- Sunil Singhal November 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

First of all, it's obvious that the second new operation in enqueue() and dequeue() method is rebundant.
Then, as copy is so often to do, I suggest that your PersistentQueue implement Cloneable interface. Thus the way of creating a copy in enqueue() and dequeue() method could use clone() method, which is faster than new. This might be an improvement.

public class PersistentQueue<E> implements Cloneable{ 
    private List<E> queue;
    public PersistantQueue<E> clone(){
        PersistantQueue<E> copy = null;
        try{
            copy = (PersistantQueue<E>)super.clone();
            copy.queue = (list<E>)this.queue.clone();
        }
        catch(CloneNotSupportedException e){
            e.printStackTrace();
        }
        return copy;
    }
    public PersistantQueue<E> enqueue(E e){ 
        if(e == null){ 
            throw new IllegalArgumentException(); 
        } 
        List<E> copy = clone(); 
        copy.add(e); 
        return copy;
    } 
    public PersistantQueue<E> dequeue(){ 
        if(queue.isEmpty()){ 
            throw new NoSuchElementException(); 
        } 
        List<E> copy = clone(); 
        copy.remove(0);
        return copy;
    } 
}

- uuuouou November 30, 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