Ebay Interview Question for Quality Assurance Engineers


Team: Data Structures
Country: United States
Interview Type: Phone Interview




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

public class ListIterator<T> implements Iterator<T> {

    private int counter = 0;

    private List<T> numbers = new ArrayList<>();

    public ListIterator(List<T> numbers) {
        this.numbers = numbers;
    }

    @Override
    public boolean hasNext() {
        return counter < numbers.size();
    }

    @Override
    public T next() {
        return numbers.get(counter++);
    }

    @Override
    public void remove() {
        if (numbers.get(counter) == null) {
            throw new UnsupportedOperationException();
        }
        numbers.set(counter, null);
    }

    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6));
        ListIterator<Integer> iterator = new ListIterator<>(numbers);
        int i = 2;
        while (iterator.hasNext()) {
            if (i-- > 0) {
                iterator.remove();
            }
            System.out.println("iterator.next() = " + iterator.next());
        }
    }

}

- CTS July 02, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This implementation is not thread safe - you need to add locks to achieve this.

- Alex M. February 04, 2017 | 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