Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

Too easy

class FlatIterator implements Iterator {
    Iterator curI;
    LinkedList<Iterator> stack;

    public FlatIterator(Iterator i) {
        curI = i;
        stack = new LinkedList<Iterator>();
    }

    @Override
    public boolean hasNext() {
        if (curI.hasNext()) {
            return true;
        }
        if (!stack.isEmpty()) {
            curI = stack.pop();
            return hasNext();
        } else {
            return false;
        }

    }

    @Override
    public Object next() {
        final Object next = curI.next();
        if (next instanceof Iterator) {
            stack.push(curI);
            curI = (Iterator) next;
            return next();
        } else {
            return next;
        }
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException("Not required");
    }
}

- John Nash: January 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.anand.learn.iter;

/**
 * @author akeshri
 *
 */
public interface MyIterable<E> {
	
	public MyIterator<E> iterate();

}

package com.anand.learn.iter;

/**
 * @author akeshri
 *
 */
public interface MyIterator<E> {
	
	public boolean hasNext();
	
	public E next();

}

/**
 * 
 */
package com.anand.learn.iter;

import java.util.List;

/**
 * @author akeshri
 * @param <E>
 *
 */
public class MyObject<E> implements MyIterable<E>  {

	/* (non-Javadoc)
	 * @see com.anand.learn.iter.MyIterable#iterate()
	 */
	@Override
	public MyIterator<E> iterate() {
		
		return (MyIterator<E>) new MyIteratorImpl<E>();
	}
	
	class MyIteratorImpl<E> {
		
	}
	
	
	public void loopIter(MyIterator<E> iter) {
		if (iter != null ) {
			while (iter.hasNext()) {
				if(iter.next() instanceof MyIterator){
					//loop over Iterator ot read the object
				} else {
					//print the object
				}
			}
		}
	}

}

- Anand January 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi, I dont think this answers the question. It specifically asks for hasNext() and next(). Shouldn't the constructor take a Iterator<Iterator<E>> as a parameter, and keep a reference to the current iterator, when that iterator reaches to an end, then switch to the next iterator?

- Guy January 16, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What do you mean by "flattens"? Can you explain the question more specifically

- <--> January 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package testing;

import java.util.Iterator;
import java.util.List;

public class AddedIteratots {

	private List <Iterator<?>> iteratots;
	private Iterator<?> currentit;
	private int currentItIndex = 0;
	public AddedIteratots(List <Iterator<?>> iteratots){
		this.iteratots = iteratots;
		currentit = iteratots.get(0);
	}
	
	public boolean hasNext(){
		boolean ret = false;
		if(currentit.hasNext())
			ret=currentit.hasNext();
		else{
			currentit = iteratots.get(++currentItIndex);
			if(currentit != null)
				return hasNext();
		}
		return ret;
	}
	
	public Object next(){
		Object ret = null;
		if(currentit.hasNext())
			ret = currentit.next();
		else {
			currentit = iteratots.get(++currentItIndex);
			if(currentit != null)
				return next();
		}
		return ret;
	}
}

- Ravi January 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if the input is an Iterator of Iterator, so instead of List<Iterator<E>>, it would be Iterator<Iterator<E>>?

- Guy January 18, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

public class FlattenedIterator<K> implements Iterator<K> {
	private List<Iterator<K>> itrs;
	private volatile Iterator<K> currentItr;
	private volatile int currentItrIndex;

	public FlattenedIterator(List<Iterator<K>> itrs) {
		if(itrs != null) {
			this.itrs = itrs;
			this.currentItr = itrs.get(0);
		} else {
			this.itrs = new ArrayList<Iterator<K>>(0);
			this.currentItr = null;
		}
		this.currentItrIndex = 0;
	}
	
	@Override
	public boolean hasNext() {
		if(this.currentItr == null) {
			return false;
		}
		do {
			if(this.currentItr.hasNext()) {
				break;
			} else {
				if(currentItrIndex + 1 < itrs.size()) {
					this.currentItr = itrs.get(++currentItrIndex);
				}
			}
		}while(currentItrIndex + 1 < itrs.size());
		return this.currentItr.hasNext();
	}

	@Override
	public K next() {
		if(this.currentItr == null) {
			return null;
		}
		do {
			if(this.currentItr.hasNext()) {
				return this.currentItr.next();
			} else {
				if(currentItrIndex + 1 < itrs.size()) {
					this.currentItr = itrs.get(++currentItrIndex);
				}
			}
		} while(currentItrIndex + 1 < itrs.size());
		return this.currentItr.next();
	}

	@Override
	public void remove() {
		if(this.currentItr != null) {
			do {
				if(this.currentItr.hasNext()) {
					this.currentItr.remove();
					return;
				} else {
					if(currentItrIndex > 0) {
						this.currentItr = itrs.get(--currentItrIndex);
					}
				}
			} while(currentItrIndex > 0);
			this.currentItr.remove();
		}
	}
}

- hinax January 17, 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