Longest Shared Package Path Across Android Import Statements
Last updated: November 29, 2026
Quick Overview
Given a list of dotted Java package names, return the longest dotted prefix made of whole segments that all of them share.
0
0
0 solved
Given a list of dotted Java package names, return the longest dotted prefix made of whole segments that all of them share.
A short Google phone screen question for Android tooling roles, where a build tool wants a common root package. It checks segment-level rather than character-level comparison and clean handling of trivial inputs.
What the Interviewer Expects
- Split names on dots and compare whole segments
- Use a segment trie or pairwise reduction
- Stop when segments differ or a name ends
- Avoid returning partial segments like com.goo
- Handle empty input and a single name
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you find the most common prefix shared by at least 80 percent of imports?
- How would you group imports by their longest shared prefix with at least one other import?
- What if the list is too large for memory?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: names, a list of strings like com.google.android.gms.maps. Output: the longest string of the form s1.s2...sk such that every name begins with those exact k segments. Return an empty str...
High-Level Design
Character-level longest common prefix would return com.google.android.g for com.google.android.gms and com.google.android.gsf, which is not a valid package. So treat each segment as one symbol. ...