FastPrepMost Visited Sprint Marker

Most Visited Sprint Marker

Microsoft logoMicrosoft● MediumFULLTIMEOA

Problem statement

Pat is training to become a great runner. Pat runs sprints along a straight trail with markers numbered from 1 to n. The coach gives a list sprints of markers that Pat must reach in order.

The training is a series of sprints. Pat runs from sprints[0] to sprints[1], then from sprints[1] to sprints[2], and so on until the last marker in the list. A sprint can move toward higher or lower marker numbers.

  • A marker is visited if Pat starts at it, stops at it, or passes through it during a sprint.
  • Visits add up over all sprints. A marker where one sprint stops and the next sprint starts is counted once for each of those two sprints.

Return the lowest-numbered marker that Pat visits the most times.

Function

getMostVisited(n: int, sprints: int[]) → int

Examples

Example 1

n = 5sprints = [2,4,1,3]return = 2

Pat runs three sprints.

  • The sprint from 2 to 4 visits markers 2, 3, and 4.
  • The sprint from 4 to 1 visits markers 4, 3, 2, and 1.
  • The sprint from 1 to 3 visits markers 1, 2, and 3.

The visit counts for markers 1 through 5 are 2, 3, 3, 2, and 0. Markers 2 and 3 tie with 3 visits, so the answer is the lower one, 2.

Example 2

n = 7sprints = [6,3,5]return = 3

Pat runs two sprints.

  • The sprint from 6 to 3 visits markers 6, 5, 4, and 3.
  • The sprint from 3 to 5 visits markers 3, 4, and 5.

The visit counts for markers 1 through 7 are 0, 0, 2, 2, 2, 1, and 0. Markers 3, 4, and 5 tie with 2 visits, so the answer is 3.

Constraints

  • 2 <= n <= 10^5
  • 2 <= sprints.length <= 2 * 10^4
  • 1 <= sprints[i] <= n
  • sprints[i] != sprints[i + 1]

More Microsoft problems

See Microsoft hiring insights
Saved
public int getMostVisited(int n, int[] sprints) {
    // Write your code here.
}
Checking account…