Google Interview Question for Software Engineer in Tests


Team: Ads
Country: United States
Interview Type: Written Test




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

public class MyIterator<T>{
  T[] arr;
   int currPos;
   int negPos;
   int size;
  public boolean hasNext(){
    for(int i = currPos+1; i<size;i++){
        if(arr[i] < 0){
            if(negPos != i) negPos = i;
            return true;
        }
    }
   return false;
  }
  public T next(){
       if(hasNext()){
           currPos = negPos;
            return a[currPos];
       }
   return null;
 }
}

- CnyrdSnyrd November 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

multiple invocation of hasNext() with out next() call traverse the collection where as it should not.

- Anonymous December 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Why not store the indexes of negative elements in a LinkedList. That should do it for this question.

- Bevan December 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public SomeClass extends SomeListImplementation<Integer> implements List<Integer> {

	...

	puiblic Iterator<Integer> iter() {

	return new Iterator<Integer>() {

		private int i = -1;
		private int toRemove = -1;

		@Override
		public boolean hasNext() {
			if (i == -1) {
				i == 0;
			}
			moveForward();

			return i < size();
		}

		private int moveForward() {
			while(i < size() && get(i) >= 0) {
				i++;
			}
		}

		@Override
		public Integer next() {
			if (!hasNext())	{
				throw new NoSuchElementException();
			}

			Integer result = get(i);
			toRemove = i;
			i++;
			return result;
		}

		@Override
		public Integer remove() {
			if (toRemove == -1) {
				throw new IllegalStateException();
			}

			remove(i);
			i = -1;
		}
	}

}

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

package cc.goog;

import java.util.Collection;
import java.util.Iterator;

public class IteratorForNegetiveNumbers implements Iterator<Integer> {
	
	private Iterator<Integer> i;
	
	private Integer lastRead = null;

	public IteratorForNegetiveNumbers(Collection<Integer> c) {
		this.i = c.iterator();
	}
		
	@Override
	public boolean hasNext() {
		while (i.hasNext()) {
			Integer x = i.next();
			if (x != null && x < 0) {
				lastRead = x;
				return true;
			}
		}
		return false;
	}

	@Override
	public Integer next() {
		if (lastRead != null) {
			lastRead = null;
			return lastRead;
		}
		while (true) {
			Integer x = i.next();
			if (x != null && x < 0) {
				return x;
			}
		}
	}

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

- A February 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Iterator { 
int i = 0, n = a.size(); 
bool hasNext() {
       while (i < n) {
            if (a[i] < 0) break; 
       }
      return i < n;
}
int next() 
{
         if (i < n) return a[i++];
}

}

- Anonymous August 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Iterator { 
int i = 0, n = a.size(); 
bool hasNext() {
       while (i < n) {
            if (a[i] < 0) break;
            else i++; 
       }
      return i < n;
}
int next() 
{
         return i < n ? a[i++] : null; 
}

}

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

class negativeIterator<Integer> {
private Iterator<Integer> it;
negativeIterator( Iterator<Integer> iit) { this.it=iit; }
public boolean hasNext() {
while(it.hasNext()) {
if(it.next()<0) return true;
}
return false;

}
public Integer next() {
return it.next();
}



}

- freemail165 February 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

public class negativeIterator {
private Iterator<Integer> it;
private int cur;
public negativeIterator( Iterator<Integer> iit) { this.it=iit; }
public boolean hasNext() {
while(it.hasNext()) {
this.cur=(java.lang.Integer) it.next();
if(this.cur<0) return true;
}
return false;
}
public int next() {
return this.cur;
}
}

- freemail165 February 27, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class NegativeIterator implements Iterator<Integer> {

	private Iterator<Integer> originalIterator;

	private Integer cachedNext = null;

	public NegativeIterator(Iterable<Integer> iterable) {
		if(iterable == null) {
			throw new IllegalArgumentException("iterable is null");
		}

		originalIterator = iterable.iterator();
	}

	@Override
	public boolean hasNext() {
		if(cachedNext != null) {
			return true;
		}

		while(originalIterator.hasNext()) {
			Integer next = originalIterator.next();

			if(next < 0) {
				cachedNext = next;
				return true;
			}
		}

		return false;
	}

	@Override
	public Integer next() {
		if(hasNext() == false) {
			throw new IllegalStateException();
		}

		Integer result = cachedNext;
		cachedNext = null;
		return result;
	}
}

- Carlos June 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Python version

# -*- coding: utf-8 -*-
"""
Created on Sun Nov 25 18:12:14 2012

@author: qzhang
"""

class MyIterator:
    def __init__(self,array):
        self.array = array
        self.currPos = 0
        self.size = len(array)
        self.negPos = 0
        
    def hasNext(self):
        for i in range(self.currPos +1,self.size,1):
            if(self.array[i] <0):
                if(self.negPos != i):
                    self.negPos = i
                return True
        return False
    
    def next(self):
        if(self.hasNext()):
            self.currPos = self.negPos;
            return self.array[self.currPos]
        return
        

if __name__ == "__main__":
    array = [1,2,4]
    iter1 = MyIterator(array)            
    print iter1.hasNext()
    print iter1.next();

- John November 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

def hasNext(self):
        if self.currPos != self.negPos: return True
        for i in range(self.currPos +1,self.size,1):
            if(self.array[i] <0):
                if(self.negPos != i):
                    self.negPos = i
                return True
        return False

- TomHoward November 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

If additional data structure is allowed, can't we have a LinkedList(LL) representing just the negative numbers of the collection?
For hasNext(), we just need to see if next element of LL is not null.
For next(), we check with hasNext(), and then return the next element from LL.
Note: The only concern is desired mutation required for the LL, i.e., if we are performing hasNext and someone adds a negative number to the collection, it has to be inserted at proper place in LL as well.

- Learner November 27, 2012 | 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