Google Interview Question


Country: United States
Interview Type: In-Person




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

public class RoundRobinIterator {
    List<Iterator> iterators;
    int index;  //current element
    int size;

    public RoundRobinIterator(List<Iterator> iter) {
        iterators = iter;
        size = iterators.size();
        index = 0;
        if(size > 0) locateNext();
    }

    public Object next() {
        locateNext();
        if(size == 0) return null;
        return iterators.get(index).next();
    }

    public boolean hasNext() {      // tradeoff - O(1) or O(n) ?
        if(size == 0) return false;
        return true;

    }

    private void locateNext() {
        if(iterators.get(index).hasNext()) {
            index = (index + 1) % size;
        }
        while(size > 0 && !iterators.get(index).hasNext()) {
            Iterator tmp = iterators.get(index);
            iterators.set(index, iterators.get(size - 1));
            iterators.set(size - 1, tmp);
            size--;
            if(index == size) index = 0;
        }
    }

}

- aonecoding February 27, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Looking for interview questions sharing and mentors? Visit A++ Coding Bootcamp at aonecode.com (select english at the top right corner).
We provide ONE TO ONE courses that cover everything in an interview from the latest interview questions, coding, algorithms, system design to mock interviews. All classes are given by experienced engineers/interviewers from FB, Google and Uber. Help you close the gap between school and work. Our students got offers from G, U, FB, Amz, Yahoo and other top companies after a few weeks of training.
Welcome to email us with any questions.

public class RoundRobinIterator {
    List<Iterator> iterators;
    int index;  //current element
    int size;

    public RoundRobinIterator(List<Iterator> iter) {
        iterators = iter;
        size = iterators.size();
        index = 0;
        if(size > 0) locateNext();
    }

    public Object next() {
        locateNext();
        if(size == 0) return null;
        return iterators.get(index).next();
    }

    public boolean hasNext() {      // tradeoff - O(1) or O(n) ?
        if(size == 0) return false;
        return true;

    }

    private void locateNext() {
        if(iterators.get(index).hasNext()) {
            index = (index + 1) % size;
        }
        while(size > 0 && !iterators.get(index).hasNext()) {
            Iterator tmp = iterators.get(index);
            iterators.set(index, iterators.get(size - 1));
            iterators.set(size - 1, tmp);
            size--;
            if(index == size) index = 0;
        }
    }

}

- aonecoding February 27, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Too complex impl presented, we can do much simpler:

def Sample : { 
   $$ : def(){
      $.its = list([0:random(4) + 1 ]) ->{
        l = list([0:random(5)]) ->{ random(100) } 
        println(l)
        l.iterator()
      }
      $.index = -1 
   },
   hasNext : def(){ 
     while ( !empty($.its) ) {
       $.index =  ($.index + 1) % size( $.its )
       it = $.its[$.index]
       if ( it.hasNext ) { return true }
       $.its.remove( $.index )
       $.index -= 1 
     }
     false 
   },
   next : def(){ return $.its[ $.index].next() }
}

s = new ( Sample )
while ( s.hasNext() ){
  printf(' %s ', s.next())
}
println()

- NoOne February 27, 2017 | 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