Google Interview Question for Interns Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

Good coding question for an interview.

class Peekable <T> : Iterator <T> {
    public Peekable(Iterator <T> iterator) {
        _iterator = iterator;
        getTop();
    }
     
    public bool hasNext() {
        return top != null;
    }
    
    public T next() {
        if (top == null) throw EndOfStreamException();
        T currentTop = _top;
        getTop();
        return currentTop;       
    }
      
    public T peek() {
        return _top; 
    }
      
    private getTop() {
        _top = null;
        if (_iterator.hasNext()) {
            _top = _iterator.next();
        }
    }
    
    private Iterator <T> _iterator;
    private T _top;
}

- Anonymous April 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

This solution doesnt account for Iterators that contain nulls

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

Replace null with a boolean flag. Essentially correct.

- Anonymous April 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Iterator is an interface which each of the collection classes implement in their own way which includes the implementation of the 3 methods mentioned. It appears like you are being asked to create your own collection type because unless you know what your collection looks like, it doesnt make sense to implement these methods.
Please correct me if I'm wrong.

- teli.vaibhav April 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes you are correct. I was asked to implement my own class

- JustYourAverageDev April 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

So like say with an ArrayList or LinkedList or a Stack or anything in the java API or for that matter even of our own custom API, it is possible to implement these methods because we know how our class looks. Did you happen to find out what kind of a class implementation was expected?

- teli.vaibhav April 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

and

public class PeekableIterator<T> implements Iterator<T> {
private final Iterator iterator;
private boolean isPeak = false;
private T nextPeek = null;

public PeekableIterator(Iterator<T> iterator) {
this.iterator = iterator;
}

@Override
public T next() {
if (isPeak) {
isPeak = false;
return peek();
}
return iterator.next();
}

@Override
public void remove() {
iterator.remove();
}

@Override
public boolean hasNext() {
return iterator.hasNext();
}
public T peek() {
if (hasNext()) {
if (isPeak == false) {
nextPeek = next();
isPeak = true;
}
} else {
nextPeek = null;
}
return nextPeek;
}

}

and

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

Same as the previous() method in the Java list iterators.

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

could you please explain the significance of

: Iterator <T>

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

I present a simple solution in Python below. Using a double-ended queue for O(1) pop/append times.

Solution:

from collections import deque
class PeekIterator:
  def __init__(self, nums):
    self.result = deque(nums)

  def peek(self):
    return self.result[0]

  def next(self):
    return self.result.popleft()

  def hasNext(self):
    return len(self.result) > 0

Test code:

p = PeekIterator([2,3,6,7,10,12])
while p.hasNext():
  print('Elem at top of stack = ', p.peek(), end=', ')
  print('and now popping elem using next: ', p.next())
'''
Output:
Elem at top of stack =  2, and now popping elem using next:  2
Elem at top of stack =  3, and now popping elem using next:  3
Elem at top of stack =  6, and now popping elem using next:  6
Elem at top of stack =  7, and now popping elem using next:  7
Elem at top of stack =  10, and now popping elem using next:  10
Elem at top of stack =  12, and now popping elem using next:  12
'''

- prudent_programmer March 18, 2018 | 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