Yahoo Interview Question for Software Engineer / Developers


Team: Ad
Country: United States
Interview Type: In-Person




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

explain what iterator is in algorithms?

- Anonymous March 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

iterator with next(), hasnext(). If you do not understand that, you need to do some reading.

It is more programming, not algorithms, though.

- Anonymous March 20, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class IteratorToSkipZero {

int current;
int[] list;

public static void main(String[] args) {
int[] input = {1,0,0,0,0,6,0};
IteratorToSkipZero it = new IteratorToSkipZero(input);
System.out.println(it.next());
System.out.println(it.next());
System.out.println(it.hasNext());
}

public IteratorToSkipZero(int[] l){
list = l;
current = 0;
}

public boolean hasNext(){
skipZero();
if(current == (list.length)){
return false;
}
return true;

}

public int next(){

int o = 0;
if(hasNext()){
o = list[current];
current++;
return o;
}
throw new UnsupportedOperationException();

}

private void skipZero() {
while(current < list.length){
if(list[current] == 0){
current ++;
} else {
break;
}
}
}
}

- Deepanwita Roy March 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.test.integer;

public class GreaterThanZeroIntIterator {
private int[] intValues;

private int curIndex = 0;

private boolean hasNext = false;

public GreaterThanZeroIntIterator(int[] intValues) {
super();
this.intValues = intValues;

fetchNext();
}

public static void main(String[] args) {
int[] intArr = new int[] { 1, 2, 3, 0, 1, 2, -3 };

GreaterThanZeroIntIterator it = new GreaterThanZeroIntIterator(intArr);
while (it.hasNext()) {
System.out.println(it.next());
}

}

public boolean hasNext() {
return hasNext;
}

public int next() {
int result = intValues[curIndex++];
fetchNext();

return result;
}

private void fetchNext() {
hasNext = false;

while (curIndex < intValues.length) {
if (intValues[curIndex] != 0) {
hasNext = true;
break;
} else {
curIndex++;
}
}
}
}

- Anonymous March 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class IntegerStream  implements Iterable{

    private Node head;

    @Override
    public Iterator iterator() {
        return null;
    }

    class Node{

        private Integer data;
        private Node next;
    }

    class IntegerStreamIterator implements Iterator {

        Node curr = head;
        @Override
        public boolean hasNext() {
            return curr != null;
        }

        @Override
        public Object next() {
            while(curr.data == 0){
                curr = curr.next;
            }
            Integer item = curr.data;
            curr = curr.next;
            return item;
        }
    }
}

- Spectral July 18, 2016 | 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