Smallest Common Integer in Sorted Lists
Reported by candidates from Motive's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Motive reportedly served this one in July 2019, and the constraints do the talking: up to 200000 integers across 100 lists, values up to a billion. You can't build a value-indexed array, and checking every candidate against every list by linear scan is wasteful. The input is sorted, and that's the whole hint. It's a pointer-walk problem, close to a k-way merge or a count-by-list approach. If you've got the OA in a day or two, learn the pointer approach cold. StealthCoder sits invisibly as a safety net if your mind goes blank mid-assessment, but the idea is simple enough to own.
The problem
You are given lists, a collection of integer lists. Every inner list is sorted in nondecreasing order. Return the smallest integer that appears in every inner list. Repeated copies within one list count as one presence. If no integer appears in every list, return -1. Function smallestCommonInteger(lists: int[][]) → int Examples Example 1 lists = [[1,2,3,4],[0,2,4],[2,5,9]] return = 2 The value 2 appears in all three lists, and no smaller value does. Example 2 lists = [[1,2],[3,4]] return = -1 The two lists have no common integer. Example 3 lists = [[0,0,1],[0,2],[0,0,3]] return = 0 Duplicate copies do not matter; 0 is present in every list. Constraints 1 <= lists.length <= 100. 1 <= lists[i].length, and the total number of integers is at most 200000. 0 <= lists[i][j] <= 1000000000. Every inner list is sorted in nondecreasing order.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that sorted lists let you advance pointers instead of searching. Keep one pointer per list. Take the max of the current values across all lists. For each list, move its pointer forward while its value is below that max. If every list now shows the same value, that's your answer, and it's the smallest because you never skipped past a shared value. If any pointer runs off the end, return -1. Total work is bounded by the total number of integers, plus a max scan per round. The common pitfall is mishandling duplicates. Pointer advancement handles them naturally, but a counting hash map breaks if you count a repeat twice in one list. Another pitfall is forgetting the -1 case when a list is exhausted. StealthCoder is the hedge if you freeze live, but trace Example 3 by hand first.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Smallest Common Integer in Sorted Lists cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Motive's OA.
Motive reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Smallest Common Integer in Sorted Lists FAQ
What's the trick for Smallest Common Integer in Sorted Lists?+
Use one pointer per list. Find the current max across the pointers, then advance every list past values smaller than it. When all pointers show the same value, return it. If any list runs out, return -1. Sorted order guarantees you never skip a valid answer.
Can I use a hash map instead?+
Yes. Count how many distinct lists contain each value, deduplicating within a list, then return the smallest value with count equal to the number of lists. It works within the 200000 total size, but it uses extra memory and needs a sort or min scan. The pointer method is leaner.
How do duplicates affect the answer?+
They don't, as long as you handle them. Example 3 has repeated zeros, and 0 is still the answer. With pointers, repeats are skipped naturally. With a map, dedupe per list with a set or by comparing to the previous element.
How hard is this one really?+
Easy to medium. The logic is short once you notice the sorted input. Most mistakes come from edge cases: a single list, an exhausted list, or duplicates. Write the loop on paper once and the code follows quickly.
How do I prepare in 48 hours for this Motive OA?+
Code the pointer solution from memory twice, then the hash map version as a fallback. Test on all three examples plus a single-list input. Also review related sorted-input problems like intersecting two arrays. Don't memorize code, memorize the invariant.