Skip to the code
cf.
+10 XP

#567 Longest Shared Run of Events

easy

Two user sessions are recorded as arrays of event ids, a and b. An analyst wants the longest run of events that appears in both sessions back to back, with nothing in between (a common contiguous subarray).

Write longestSharedRun(a, b) that returns its length, or 0 if the sessions share no event.

Example 1

Input: a = [4,7,1,8,2,5], b = [9,1,8,2,6]
Output: 3

[1, 8, 2] appears in both, back to back.

Example 2

Input: a = [1,2], b = [3,4]
Output: 0

No event is shared.

Constraints

  • 1 <= a.length, b.length <= 1000
  • 0 <= a[i], b[i] <= 100
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).