Card Flipping Game - Problem
Card Flipping Game

You're given two arrays representing playing cards on a table. Each card has a number on both sides - one facing up (fronts) and one facing down (backs). Your goal is to find the smallest number that can be completely hidden by flipping some cards.

Rules:
• Initially, all cards show their front numbers
• You can flip any cards to show their back numbers instead
• A number is good if it appears face-down somewhere AND never appears face-up
• Return the smallest good number, or 0 if impossible

Example: If fronts = [1,2,4,4] and backs = [1,3,4,1], you could flip the first card to hide all 1s (making 1 good), or flip cards to make 3 good (since 3 only appears on backs).

Input & Output

example_1.py — Basic Case
$ Input: fronts = [1,2,4,4], backs = [1,3,4,1]
Output: 2
💡 Note: We can flip the first card to make 1 face down, and 1 doesn't appear on any front after flipping. However, we can also consider other numbers. The number 2 appears only on front of second card, but we need it to appear on some back to be good. Actually, let me recalculate: we need numbers that appear on backs but can be made invisible on fronts. The number 3 appears only on backs[1], and number 1 appears on backs[3]. Both can potentially be good, but 1 is smaller, so answer should be 1. Wait, let me check the constraint again - if we flip card 0, front becomes back[0]=1 and back becomes front[0]=1, so 1 still shows. Actually, since fronts[0]=backs[0]=1, card 0 will always show 1 regardless of flipping. So 1 is impossible. The answer is 2.
example_2.py — No Solution Case
$ Input: fronts = [1], backs = [1]
Output: 0
💡 Note: The only card has 1 on both sides, so 1 will always be visible regardless of flipping. No number can be made good, so return 0.
example_3.py — Multiple Options
$ Input: fronts = [1,2,3], backs = [3,2,1]
Output: 0
💡 Note: Card 1 has 2 on both sides, making 2 impossible. Numbers 1 and 3 appear on backs, but both also must appear on fronts after any flipping strategy. Since every number appears on both fronts and backs, and one card always shows the same number on both sides, we cannot hide any number completely. Return 0.

Visualization

Tap to expand
CURSED CARDBoth sides: 7❌ Never goodNORMAL CARDFront: 3, Back: 1✓ Can hide 1NORMAL CARDFront: 5, Back: 2✓ Can hide 2WINNERmin(1,2) = 1
Understanding the Visualization
1
Identify Cursed Cards
Find cards that show the same number on both sides - these numbers can never be hidden
2
Find Hidden Treasures
Look for numbers that appear on card backs but aren't cursed
3
Choose the Smallest
Among all valid candidates, pick the minimum number
Key Takeaway
🎯 Key Insight: Numbers that appear on both sides of the same card are 'cursed' and can never be completely hidden, no matter how you flip the cards!

Time & Space Complexity

Time Complexity
⏱️
O(n)

Two linear scans: one to find impossible numbers, one to find minimum valid back number

n
2n
Linear Growth
Space Complexity
O(n)

Hash set to store impossible numbers, which can be at most n numbers

n
2n
Linearithmic Space

Constraints

  • n == fronts.length == backs.length
  • 1 ≤ n ≤ 1000
  • 1 ≤ fronts[i], backs[i] ≤ 2000
  • All integers are positive
  • Each card has exactly two sides with potentially different numbers
Asked in
Google 15 Amazon 8 Meta 6 Microsoft 4
23.5K Views
Medium Frequency
~18 min Avg. Time
547 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen