Skip to content

Practice Last Visited Integers

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#2899
LeetCode Problem

Last Visited Integers

Problem Statement

Process nums in order, remembering positive integers from most recent to oldest. For a -1, let k be its position in the current consecutive run of -1 values. Append the kth most recently seen positive integer to the answer, or -1 if fewer than k have been seen. A positive value starts a new run. Return the answers for all -1 entries.

Source: Last Visited Integers on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [1,2,-1,-1,-1]
Output
[2,1,-1]
Explanation

The remembered positive values are [1, 2]. The first -1 in the consecutive run asks for the most recent value, 2; the second asks for the next value back, 1. The third asks for a third positive value, which does not exist, so it contributes -1. These lookups do not remove values from history. Track the length of the current -1 run separately from the list of positive values.

# Example 2

Input
nums = [1,-1,2,-1,-1]
Output
[1,2,1]
Explanation

After reading 1, the first -1 requests the newest positive value and returns 1. Reading 2 extends the remembered history to [1, 2] and resets the consecutive -1 counter. The next two requests therefore return 2 and then 1. Resetting the request counter is essential: a positive value starts a fresh backward traversal of the complete history, rather than clearing that history.

Constraints

1 <= nums.length <= 100
nums[i] == -1 or 1 <= nums[i] <= 100

How to solve Last Visited Integers

Original notes for interview preparation, written for this problem. Reviewed .

Key insight

Two pieces of state suffice: the list of positive integers seen so far and a counter for the current run of -1 entries. The kth -1 in a run reads the kth element from the end of the list, and a positive value resets only the counter, never the list.

Why brute force is not enough

Rescanning the prefix backward for each -1, skipping other -1 entries until the kth positive value appears, is O(n) per query and O(n^2) overall. With nums.length at most 100 that is harmless, but it hides the simple state machine the interviewer wants to hear.

Approach

  1. Keep an ordered list of positive integers in arrival order, most recent at the end, and a counter k for consecutive -1 values, initially zero.
  2. On a positive value, append it and reset k to zero. The invariant is that k equals the number of -1 entries since the last positive value.
  3. On a -1, increment k; if the list has at least k values, output the element at position length - k, otherwise output -1. Lookups never modify the list.
  4. Return the outputs after the whole array is processed.

Walkthrough of Example 2

nums = [1,-1,2,-1,-1] → [1,2,1]

  1. Read 1: list [1], k = 0.
  2. Read -1: k = 1; position 1 - 1 = 0 holds 1. Output [1].
  3. Read 2: list [1, 2], k resets to 0.
  4. Read -1: k = 1; position 2 - 1 = 1 holds 2. Output [1, 2].
  5. Read -1: k = 2; position 2 - 2 = 0 holds 1. Final output [1, 2, 1].

Time and space

Time O(n) · Space O(n). Each of the n entries takes constant work, and the history list plus the output hold at most n values together.

Interview pitfalls

  • Clearing the history on a positive value; only the run counter resets, so earlier positives stay reachable.
  • Forgetting to reset the counter on a positive value, making the next -1 reach too far back.
  • Removing the value a -1 reads; a later -1 in the same run must be able to go one step further.
  • Off-by-one in the backward index: the first -1 of a run reads the last element, so the position is length minus k.
  • Emitting an output for positive values; the answer has one entry per -1 only.

Follow-up questions interviewers ask

  • If k frequently exceeds the history size on very long inputs, how could you cap what you store?
  • How would a stack model the variant where each -1 consumes the value it reads?
  • Can you solve it in O(1) extra space if only the final answer is required?

Related LeetCode Problems

Practice more problems similar to Last Visited Integers.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.