FastPrepElements After the First Target

Elements After the First Target

Apple logoApple● EasyNEW GRADPHONE SCREEN
Learn

Problem statement

Given the integer list values and an integer target that occurs in it, return every element strictly after the first occurrence of target, preserving order.

For this exercise, values and target are non-null integers. If target occurs more than once, use its first occurrence. A target at the final position produces an empty list.

What the interview report shared

The original task allowed generic objects. Its examples included getFollowingElements([1,2,3,4], 2) -> [3,4] and getFollowingElements([a,b,c,d,e], e) -> []. A null target was reported to raise an error. The executable practice scope uses the integer example family and excludes null inputs.

Function

getFollowingElements(values: int[], target: int) → int[]

Examples

Example 1

values = [1,2,3,4]target = 2return = [3,4]

The elements after the first 2 are 3 and 4.

Example 2

values = [5,6,5,7]target = 5return = [6,5,7]

The first occurrence determines the suffix; a later occurrence is retained as an ordinary element.

Example 3

values = [1,2]target = 2return = []

No elements follow the final entry.

Constraints

  • 1 <= values.length <= 10^5.
  • -10^9 <= values[i], target <= 10^9.
  • target occurs at least once.

More Apple problems

See Apple hiring insights
public int[] getFollowingElements(int[] values, int target) {
    // Write your solution here.
}
values[1,2,3,4]
target2
expected[3,4]
Checking account…