Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given a 0-indexed integer array nums of even length and there is also an empty array arr. Alice and Bob decided to play a game where in every round Alice and Bob will do one move. The rules of the game are as follows:
nums, and then Bob does the same.arr, and then Alice does the same.nums becomes empty.Return the resulting array arr.
Example 1:
Input: nums = [5,4,2,3] Output: [3,2,5,4] Explanation: In round one, first Alice removes 2 and then Bob removes 3. Then in arr firstly Bob appends 3 and then Alice appends 2. So arr = [3,2]. At the begining of round two, nums = [5,4]. Now, first Alice removes 4 and then Bob removes 5. Then both append in arr which becomes [3,2,5,4].
Example 2:
Input: nums = [2,5] Output: [5,2] Explanation: In round one, first Alice removes 2 and then Bob removes 5. Then in arr firstly Bob appends and then Alice appends. So arr = [5,2].
Constraints:
2 <= nums.length <= 1001 <= nums[i] <= 100nums.length % 2 == 0Problem summary: You are given a 0-indexed integer array nums of even length and there is also an empty array arr. Alice and Bob decided to play a game where in every round Alice and Bob will do one move. The rules of the game are as follows: Every round, first Alice will remove the minimum element from nums, and then Bob does the same. Now, first Bob will append the removed element in the array arr, and then Alice does the same. The game continues until nums becomes empty. Return the resulting array arr.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[5,4,2,3]
[2,5]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2974: Minimum Number Game
class Solution {
public int[] numberGame(int[] nums) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (int x : nums) {
pq.offer(x);
}
int[] ans = new int[nums.length];
int i = 0;
while (!pq.isEmpty()) {
int a = pq.poll();
ans[i++] = pq.poll();
ans[i++] = a;
}
return ans;
}
}
// Accepted solution for LeetCode #2974: Minimum Number Game
func numberGame(nums []int) (ans []int) {
pq := &hp{nums}
heap.Init(pq)
for pq.Len() > 0 {
a := heap.Pop(pq).(int)
b := heap.Pop(pq).(int)
ans = append(ans, b)
ans = append(ans, a)
}
return
}
type hp struct{ sort.IntSlice }
func (h *hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] }
func (h *hp) Pop() interface{} {
old := h.IntSlice
n := len(old)
x := old[n-1]
h.IntSlice = old[0 : n-1]
return x
}
func (h *hp) Push(x interface{}) {
h.IntSlice = append(h.IntSlice, x.(int))
}
# Accepted solution for LeetCode #2974: Minimum Number Game
class Solution:
def numberGame(self, nums: List[int]) -> List[int]:
heapify(nums)
ans = []
while nums:
a, b = heappop(nums), heappop(nums)
ans.append(b)
ans.append(a)
return ans
// Accepted solution for LeetCode #2974: Minimum Number Game
use std::cmp::Reverse;
use std::collections::BinaryHeap;
impl Solution {
pub fn number_game(nums: Vec<i32>) -> Vec<i32> {
let mut pq = BinaryHeap::new();
for &x in &nums {
pq.push(Reverse(x));
}
let mut ans = Vec::new();
while let Some(Reverse(a)) = pq.pop() {
if let Some(Reverse(b)) = pq.pop() {
ans.push(b);
ans.push(a);
}
}
ans
}
}
// Accepted solution for LeetCode #2974: Minimum Number Game
function numberGame(nums: number[]): number[] {
const pq = new MinPriorityQueue<number>();
for (const x of nums) {
pq.enqueue(x);
}
const ans: number[] = [];
while (pq.size()) {
const a = pq.dequeue();
const b = pq.dequeue();
ans.push(b, a);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.