Find Max Min Val Using K Elements
Reported by candidates from Service Now's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
ServiceNow asked this in March 2024, and it's a sliding window or greedy problem dressed up as optimization. You're picking K elements from a sequence and maximizing or minimizing some value derived from them. The trap is overthinking it. Most candidates waste time on brute force when the pattern is just selecting the right K items in order, or using a two-pointer sweep. StealthCoder can spot the pattern and code it live if you blank on the greedy choice.
Pattern and pitfall
The core trick here is figuring out which K elements give you the extremum (max or min). If the problem wants max, you're likely picking K items that are closest together, or the K largest/smallest values. If it's min, reverse that logic. Common pitfall: trying every combination of K items (factorial time). Better move: recognize you can solve this with a sliding window of size K, or by sorting and taking the first/last K, depending on the constraint. The greedy insight is that the answer doesn't require shuffling; K contiguous or sorted elements will always beat random picks. StealthCoder handles the indexing and edge cases while you focus on the pattern.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Find Max Min Val Using K Elements 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as sliding window maximum. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Service Now's OA.
Service Now reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Find Max Min Val Using K Elements FAQ
Is this a sorting problem or sliding window?+
Depends on the constraint. If K items must be contiguous, sliding window. If K items can be anywhere, sort and pick greedily. Read the problem statement carefully. ServiceNow variants often hide the constraint in a phrase like 'consecutive' or 'optimal subset'.
What's the time complexity you need to hit?+
O(n log n) if sorting is needed, O(n) if sliding window is enough. ServiceNow doesn't usually ask for faster. Avoid exponential backtracking. If you're stuck between two approaches, pick the one that's O(n) or O(n log n).
How do I handle the K=1 or K=n edge cases?+
K=1 means return the single extremum (max or min of the array). K=n means return the extremum of all elements. Always code these first; they're free points and they reveal the problem's intent quickly.
Is this asking for the value itself or the indices?+
Check the output spec. If it's the max/min value, return a number. If it's positions, return indices. ServiceNow often asks for the value, not positions, so default there unless told otherwise.
What's the most common mistake on this problem?+
Iterating all subsets of size K. That's O(n choose K) and will timeout. The greedy or sliding window move is always faster. If you catch yourself writing nested loops beyond two levels, stop and rethink the pattern.