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.
Move from brute-force thinking to an efficient approach using array strategy.
You are given two 0-indexed integer arrays fronts and backs of length n, where the ith card has the positive integer fronts[i] printed on the front and backs[i] printed on the back. Initially, each card is placed on a table such that the front number is facing up and the other is facing down. You may flip over any number of cards (possibly zero).
After flipping the cards, an integer is considered good if it is facing down on some card and not facing up on any card.
Return the minimum possible good integer after flipping the cards. If there are no good integers, return 0.
Example 1:
Input: fronts = [1,2,4,4,7], backs = [1,3,4,1,3] Output: 2 Explanation: If we flip the second card, the face up numbers are [1,3,4,4,7] and the face down are [1,2,4,1,3]. 2 is the minimum good integer as it appears facing down but not facing up. It can be shown that 2 is the minimum possible good integer obtainable after flipping some cards.
Example 2:
Input: fronts = [1], backs = [1] Output: 0 Explanation: There are no good integers no matter how we flip the cards, so we return 0.
Constraints:
n == fronts.length == backs.length1 <= n <= 10001 <= fronts[i], backs[i] <= 2000Problem summary: You are given two 0-indexed integer arrays fronts and backs of length n, where the ith card has the positive integer fronts[i] printed on the front and backs[i] printed on the back. Initially, each card is placed on a table such that the front number is facing up and the other is facing down. You may flip over any number of cards (possibly zero). After flipping the cards, an integer is considered good if it is facing down on some card and not facing up on any card. Return the minimum possible good integer after flipping the cards. If there are no good integers, return 0.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[1,2,4,4,7] [1,3,4,1,3]
[1] [1]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #822: Card Flipping Game
class Solution {
public int flipgame(int[] fronts, int[] backs) {
Set<Integer> s = new HashSet<>();
int n = fronts.length;
for (int i = 0; i < n; ++i) {
if (fronts[i] == backs[i]) {
s.add(fronts[i]);
}
}
int ans = 9999;
for (int v : fronts) {
if (!s.contains(v)) {
ans = Math.min(ans, v);
}
}
for (int v : backs) {
if (!s.contains(v)) {
ans = Math.min(ans, v);
}
}
return ans % 9999;
}
}
// Accepted solution for LeetCode #822: Card Flipping Game
func flipgame(fronts []int, backs []int) int {
s := map[int]struct{}{}
for i, a := range fronts {
if a == backs[i] {
s[a] = struct{}{}
}
}
ans := 9999
for _, v := range fronts {
if _, ok := s[v]; !ok {
ans = min(ans, v)
}
}
for _, v := range backs {
if _, ok := s[v]; !ok {
ans = min(ans, v)
}
}
return ans % 9999
}
# Accepted solution for LeetCode #822: Card Flipping Game
class Solution:
def flipgame(self, fronts: List[int], backs: List[int]) -> int:
s = {a for a, b in zip(fronts, backs) if a == b}
return min((x for x in chain(fronts, backs) if x not in s), default=0)
// Accepted solution for LeetCode #822: Card Flipping Game
use std::collections::HashSet;
impl Solution {
pub fn flipgame(fronts: Vec<i32>, backs: Vec<i32>) -> i32 {
let n = fronts.len();
let mut s: HashSet<i32> = HashSet::new();
for i in 0..n {
if fronts[i] == backs[i] {
s.insert(fronts[i]);
}
}
let mut ans = 9999;
for &v in fronts.iter() {
if !s.contains(&v) {
ans = ans.min(v);
}
}
for &v in backs.iter() {
if !s.contains(&v) {
ans = ans.min(v);
}
}
if ans == 9999 {
0
} else {
ans
}
}
}
// Accepted solution for LeetCode #822: Card Flipping Game
function flipgame(fronts: number[], backs: number[]): number {
const s: Set<number> = new Set();
const n = fronts.length;
for (let i = 0; i < n; ++i) {
if (fronts[i] === backs[i]) {
s.add(fronts[i]);
}
}
let ans = 9999;
for (const v of fronts) {
if (!s.has(v)) {
ans = Math.min(ans, v);
}
}
for (const v of backs) {
if (!s.has(v)) {
ans = Math.min(ans, v);
}
}
return ans % 9999;
}
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.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.