Problem · Array

Peeking Iterator

Learn this problem
MediumAtlassian logoAtlassianFULLTIMEOA

Problem statement

Process peek, next, and hasNext against one iterator over nums. Peek returns the next value without consuming it, next returns and consumes it, and hasNext reports availability. Return results as strings.

Function

runPeekingIterator(nums: int[], operations: String[]) → String[]

Examples

Example 1

nums = [1,2,3]operations = ["peek","next","peek","next","hasNext"]return = ["1","1","2","2","true"]

Peek leaves the iterator position unchanged while next advances it.

Constraints

  • 1 <= nums.length <= 10000
  • Every peek and next operation is valid.
  • There are at most 20000 operations.

More Atlassian problems

drafts saved locally
public String[] runPeekingIterator(int[] nums, String[] operations) {
  // Write your code here.
}
nums[1,2,3]
operations["peek","next","peek","next","hasNext"]
expected["1", "1", "2", "2", "true"]
checking account