Reported August 2024
Uberhash table

Count Elements

Reported by candidates from Uber's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Uber OA. Under 2s to a working solution.
Founder's read

You've got an Uber OA coming in the next couple of days and "Count Elements" is on the table. This is a deceptively simple problem that trips up candidates who overthink it or miss the edge case that makes it real. You're looking at a hash-table pattern, and the trick is understanding what "count" means in this specific context. If you blank on the approach during the live OA, StealthCoder will give you the pattern instantly. The good news: this problem has a clean, one-pass solution once you see it.

Pattern and pitfall

Count Elements typically asks you to count how many elements in an array have their consecutive value also present in that same array. For example, if you have [1, 2, 3], all three count because 1 has 2, and 2 has 3. The naive approach is O(n^2) nested loops checking membership. The real move is a hash set or hash table. Build the set in one pass, then iterate and check if element + 1 exists. O(n) time, O(n) space. The pitfall: candidates forget to handle duplicates or misread the problem as "count pairs" instead of "count elements." During the live OA, if you freeze on the exact definition, StealthCoder will clarify it for you and show the set-based solution structure.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Count 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Uber's OA.

Uber reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Elements FAQ

Is this problem actually hard or does it just sound hard?+

It's not hard once you see it. The problem is short and the solution is one hash set plus one loop. The trick is parsing the problem statement correctly. Most candidates solve it in 10-15 minutes. The edge cases are minimal.

Do I need to worry about negative numbers or duplicates?+

Duplicates don't break the hash set approach. Negative numbers are fine too. The hash set will handle both cleanly. Just make sure you're checking if element + 1 exists, not element - 1 or any other variant.

What's the most common mistake people make on this one?+

Misreading the problem. Candidates think they need to count pairs, or count consecutive sequences, or count occurrences. Read the problem twice. The other mistake: forgetting to convert the array to a set first, then doing O(n^2) lookups.

Can I solve this with sorting instead of a hash set?+

Yes, but it's slower. Sort O(n log n), then iterate and check if next element is current + 1. It works. Hash set is cleaner and faster. Uber probably expects the hash set move, but sorting is a valid fallback if you're stuck.

How do I prepare for this in 48 hours if I've never seen it?+

Read the problem statement multiple times. Code the hash set solution once by hand. Test it on 2-3 examples. That's it. This problem doesn't need a prep course. Understand the pattern, code it, move on.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Uber.

OA at Uber?
Invisible during screen share
Get it