Interview Question

Anonymous Interview Los Angeles, CA

flatten iterator

Answer

Interview Answer

2 Answers

1

(newton-raphson)

public static float findzero()
{
    float delta = 0.01f;
    float epsilon = 0.00001f;
    float x = 0.0f;
    float fxn = f(x);
    while (Math.abs(fxn) > epsilon) {
        float fxnp = (f(x+delta) - f(x-delta)) / (2 * delta);
        x = x - fxn / fxnp;
        fxn = f(x);
    }
    return x;
}

Rahul on May 5, 2013
1

(flatten iterator)

public static Iterator flattenIterator(Iterator<Iterator> nested) {
    return new FIterator(nested);
}

class FIterator {
    Iterator currentIterator = null;
    Iterator<Iterator> nested = null;

    public FIterator(Iterator<Iterator> nested) {
        this.nested = nested;
    }

    public boolean hasNext() {
        while ( ((null != currentIterator) && (!currentIterator.hasNext())) ||
                ((null != currentIterator) && (nested.hasNext()))) {
            currentIterator = nested.next();
        }
        return (currentIterator != null);
    }

    public Object next() {
        if (!hasNext()) return null;
        return currentIterator.next();
    }
}

Rahul on May 5, 2013

Add Answers or Comments

To comment on this question, Sign In with Facebook or Sign Up