Iterate NestedInteger Contents in Their Original Flattened Order

Quick Overview

Iterate NestedInteger Contents in Their Original Flattened Order. Implement NestedIterator(vector NestedInteger &), next() and hasNext(). Flatten nested lists in left-to-right depth-first order. The caller repeatedly checks ha

Iterate NestedInteger Contents in Their Original Flattened Order

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Implement `NestedIterator(vector<NestedInteger>&)`, next() and hasNext(). Flatten nested lists in left-to-right depth-first order. The caller repeatedly checks hasNext and then calls next. NestedInteger offers isInteger(), getInteger() and getList(); use these typed accessors and preserve all iterator methods. ```cpp class NestedIterator { public: NestedIterator(vector<NestedInteger> &nestedList); int next(); bool hasNext(); }; ``` ### Examples ```text NestedIterator([[],[2,[3]],4]); repeated hasNext/next yields [2,3,4] NestedIterator([[]]); hasNext() -> false ``` ### Constraints - The outer nestedList has 1 through 500 elements. - Integer values lie in [-1000000,1000000]. - No total nested-element or nesting-depth bound is supplied.

Overview: Iterate NestedInteger Contents in Their Original Flattened Order. Implement NestedIterator(vector NestedInteger &), next() and hasNext(). Flatten nested lists in left-to-right depth-first order. The caller repeatedly checks ha

Amazon logo
Amazon
Date not reported
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Implement NestedIterator(vector<NestedInteger>&), next() and hasNext(). Flatten nested lists in left-to-right depth-first order. The caller repeatedly checks hasNext and then calls next. NestedInteger offers isInteger(), getInteger() and getList(); use these typed accessors and preserve all iterator methods.

class NestedIterator {
public:
    NestedIterator(vector<NestedInteger> &nestedList);
    int next();
    bool hasNext();
};

Examples

NestedIterator([[],[2,[3]],4]); repeated hasNext/next yields [2,3,4]
NestedIterator([[]]); hasNext() -> false

Constraints

  • The outer nestedList has 1 through 500 elements.
  • Integer values lie in [-1000000,1000000].
  • No total nested-element or nesting-depth bound is supplied.