The Last Visited Integers coding problem is a simulation task. You are given an array of strings which can either be positive integers or the string "prev". You iterate through the array, keeping track of the integers you've seen in a list. When you see "prev", you need to find the integer you saw most recently. If you see consecutive "prev" strings, you look further back in your history.
General Motors and other companies use this "Easy" problem to test basic programming logic and list manipulation. It's about maintaining a history and correctly indexing into it based on a running count of "prev" occurrences. It evaluates a candidate's ability to handle simple state transitions and edge cases, such as asking for a "prev" when no integers exist or when the history is exhausted.
This utilizes the Array and Simulation interview pattern. You maintain two lists: nums (to store all integers seen so far) and ans (the result list). You also keep a variable k to count consecutive "prev" strings. When you encounter a number, you prepend it to nums (or append and access from the back) and reset k to 0. When you encounter "prev", you increment k and look at the (k-1)th element of the nums list.
Input: ["1", "2", "prev", "prev", "prev"].
[2, 1, -1].The most common mistake is not resetting the k counter when a new integer is encountered. Another mistake is incorrectly calculating the index into the history list, especially if they are appending to the end of the list rather than the beginning. Failing to return -1 for out-of-bounds "prev" requests is also a frequent oversight.
For "Array, Simulation interview pattern" questions, keep your state variables clear. Use descriptive names like consecutive_prev_count and integer_history. Writing down the state changes for a small example on a whiteboard will help you catch off-by-one indexing errors.