Custom Array Iterator
Updated
Problem statement
Implement a class ArrayIterator that walks through an integer array from the first value to the last:
ArrayIterator(int[] nums)creates the iterator. It starts before the first value ofnums.boolean hasNext()returnstrueif at least one value has not been returned yet, andfalseotherwise. It does not move the iterator.int next()returns the next value and moves past it.
To test your class, you get the array nums and a list operations of calls made, in order, on one iterator built from nums. Each call is "next" or "hasNext". Return one string for each call, in the same order: the value from next in normal decimal form (such as "-3"), or "true" or "false" from hasNext.
Function
runArrayIterator(nums: int[], operations: String[]) → String[]Examples
Example 1
nums = [8,-3,5]operations = ["hasNext","next","next","hasNext","next","hasNext"]return = ["true","8","-3","true","5","false"]The iterator starts before 8. hasNext is "true". The two next calls return 8 and then -3. One value is still left, so hasNext is "true". The last next returns 5, and now nothing is left, so hasNext is "false".
Example 2
nums = []operations = ["hasNext","hasNext"]return = ["false","false"]The array is empty, so there is never a next value. Both hasNext calls return "false".
Example 3
nums = [4,4]operations = ["next","hasNext","hasNext","next","hasNext"]return = ["4","true","true","4","false"]next returns the first 4. The two hasNext calls in a row both return "true": asking does not move the iterator, so the second 4 is still waiting. The next next returns it, and then hasNext is "false".
Constraints
0 <= nums.length <= 1000-10^5 <= nums[i] <= 10^51 <= operations.length <= 2000- Each operation is
"next"or"hasNext". nextis only called when at least one value has not been returned yet.
More Nutanix Onsite Interview questions
- Design Hit CounterONSITE INTERVIEW · Seen Oct 2026
- Peak Process MemoryONSITE INTERVIEW · Seen Aug 2026
- Minimum Operations to Transform ArrayONSITE INTERVIEW · Seen Jun 2026
- Count Connected Components in an Undirected GraphONSITE INTERVIEW · Seen Aug 2025
- LRU CacheONSITE INTERVIEW · Seen Aug 2025
- Running Median of a Multilevel Linked List in Level OrderONSITE INTERVIEW · Seen Aug 2025
- Collapse Runs of Repeated CharactersONSITE INTERVIEW · Seen Nov 2022
- Anagram Words in a SentenceONSITE INTERVIEW · Seen Apr 2022
- Sudoku SolverONSITE INTERVIEW · Seen Feb 2022
- Distance Sums to Special Nodes in a Weighted TreeONSITE INTERVIEW · Seen Aug 2021
Similar questions at other companies
- Ring Buffer OperationsAmazon · ONSITE INTERVIEW
- Apply Overridden Integer TransformsOptimus Information · ONSITE INTERVIEWOptimus Information interview questions
- Logger Rate LimiterAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Design an Ordered StreamBloomberg LP · OA · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Accumulate Numeric Value ObjectsSquarepoint Capital · ONSITE INTERVIEWSquarepoint Capital interview questions
- Customer Bootstrap APIDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions
- Round-Robin Iterator over IteratorsBridge · ONSITE INTERVIEWBridge interview questions
- Top K Largest Values from a StreamThe D.E. Shaw Group · ONSITE INTERVIEWThe D.E. Shaw Group interview questions
- Path Leaf Character IteratorGoogle · ONSITE INTERVIEWGoogle interview questions
- Randomized Collection with Duplicate ValuesOkta · ONSITE INTERVIEWOkta interview questions