← Back to section

Final exam — Array

Three unseen problems (not covered in the lessons) to test whether you leave this section able to solve any problem of the topic: one easy, one medium and one hard. Some test cases are hidden — like the real LeetCode. Solve in Python or JavaScript.

Easy

Majority Element

Given an array nums of size n, return the majority element — the element that appears more than ⌊n/2⌋ times. You may assume it always exists in the array.

Example: nums = [2, 2, 1, 1, 1, 2, 2] → answer 2 (appears 4 times out of 7).

Carregando editor…
Medium

Subarray Sum Equals K

Given an array of integers nums (with positives and negatives) and an integer k, return how many contiguous subarrays sum to exactly k.

Example: nums = [1, 1, 1], k = 2 → answer 2 (the subarrays [1,1] starting at indices 0 and 1).

Note: with negative numbers, sliding window doesn't work — think prefix sums.

Carregando editor…
Hard

Trapping Rain Water

Given an array height representing an elevation map where each bar has width 1, compute how much water gets trapped between the bars after it rains.

Example: height = [0,1,0,2,1,0,1,3,2,1,2,1] → answer 6.

The water above each position is limited by the smaller of the tallest bars to the left and to the right.

Carregando editor…