Longest Shared Road Segment Between Two Trip GPS Traces

Last updated: November 29, 2026

Quick Overview

Two completed trips are map-matched into sequences of road segment ids; return the length of the longest contiguous run of segments they share in the same order, which is used to judge whether two trips could have been pooled.

Uber
Coding & Algorithms
Software Engineer
Uber
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Two completed trips are map-matched into sequences of road segment ids; return the length of the longest contiguous run of segments they share in the same order, which is used to judge whether two trips could have been pooled.

Uber's pooling and fraud teams compare map-matched routes, for example to find riders who share most of a commute. The screen checks that you distinguish a contiguous common run (substring) from a common subsequence and write the 2D DP with O(n) space.

What the Interviewer Expects
  • Clarify contiguous (substring) versus subsequence
  • Define dp[i][j] as the common run length ending at i and j
  • Reset to zero on mismatch
  • Use one rolling row iterated carefully
  • Mention suffix automaton or rolling hash for long traces
Key Topics to Cover
2D dynamic programming
longest common substring
rolling hash
map matching
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What if you want the longest common subsequence instead, allowing detours?
  • How would you do this in O((m + n) log n) with rolling hashes and binary search?
  • How would you find all pairs of trips in a city sharing at least 5 km of road?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: lists a and b of segment ids.
  • Output: length of the longest run a[i..i+L-1] == b[j..j+L-1].
  • Same direction only; reversed traversal is a different segment id in a directed road grap...
High-Level Design

Let dp[i][j] be the length of the common run ending exactly at a[i-1] and b[j-1]. If they match, dp[i][j] = dp[i-1][j-1] + 1, else 0. The answer is the max cell. Each cell depends only on its ...


Submit Your Answer
Markdown supported