The Patterns Behind Everything ⭐

Master these five patterns and you'll solve most Java interview programs efficiently.

Pattern Code Pattern Common Problems Solved
Digit Extraction digit = n % 10; n = n / 10; Reverse number, palindrome, Armstrong, digit sum
Build Reverse reverse = reverse * 10 + digit; Reverse number, palindrome
Two Pointers start++; end--; Reverse array, reverse string, palindrome
HashSet Deduplication if (!set.add(value)) Duplicate detection, first duplicate, pair sum
HashMap Frequency Counting map.put(key, map.getOrDefault(key,0)+1) Frequency count, first non-repeated element, anagram

Array Programs (~50)

Basics & Calculations

Learn more: Array Basics & Calculations

Program Approach Time Complexity
Declare & Initialize Array int[] array = {...}
Print Array Elements Loop or Arrays.toString() O(n)
Sum of Elements Accumulate values O(n)
Average Sum then divide using double O(n)
Largest / Smallest Element Single pass tracking maximum and minimum O(n)
Second Largest Track largest and second largest simultaneously O(n)
Second Smallest Track smallest and second smallest O(n)
Maximum Difference Single traversal maintaining max and min O(n)

Sorting, Reverse & Duplicates

Learn more: Array Sorting, Reverse & Duplicates

Advertisement
Program Approach Time Complexity
Sort Ascending Bubble Sort O(n²)
Sort Descending Reverse comparison O(n²)
Reverse Array Two-pointer swapping O(n)
Duplicate Detection (Brute Force) Nested loops O(n²)
Duplicate Detection (Optimal) HashSet O(n)
Remove Duplicates LinkedHashSet O(n)
Count Frequency visited[] or HashMap O(n)
Unique Elements Frequency equals one O(n)
First Duplicate First failed HashSet.add() O(n)

Rotation, Search & Patterns

Learn more: Array Rotation & Patterns

Program Approach Time Complexity
Left Rotation Shift left and append first element O(n)
Right Rotation Shift right and prepend last element O(n)
Missing Number Expected sum − actual sum O(n)
Pair with Given Sum HashSet complement lookup O(n)
Majority Element Moore's Voting Algorithm O(n)
Move Zeros to End Write-index approach O(n)
Count Even / Odd % 2 check O(n)
Count Positive / Negative Sign comparison O(n)
Merge Arrays Copy into new array O(n+m)
Common Elements HashSet lookup O(n)
Compare Arrays Arrays.equals() or loop O(n)
Array ↔ ArrayList Conversion add() / toArray() O(n)

Number Programs (~23)

Basics & Intermediate

Learn more: Number Basics

Program Recommended Approach
Even / Odd % 2 == 0
Positive / Negative Sign comparison
Swap Two Numbers Temporary variable or arithmetic swap
Reverse Number Digit extraction + reverse construction
Count Digits Division by 10 or String.length()
Factorial Multiply values from 1 to n
Power Loop or Math.pow()
Leap Year Standard leap-year formula
Prime Number Divisibility check up to √n
Palindrome Number Reverse and compare
Armstrong Number Sum of digit cubes
Perfect Number Sum of proper divisors
Strong Number Sum of digit factorials
Fibonacci Series Two running variables

Advanced & Interview-Oriented

Learn more: Advanced Number Programs

Program Recommended Approach
GCD / LCM Euclidean Algorithm
Sum of Digits Digit extraction
Smallest / Largest Digit Track while scanning
Reverse Using Recursion Recursive digit processing
Print Numbers Recursively Recursive traversal
Decimal → Binary Divide by 2 repeatedly
Binary → Decimal Multiply accumulated result by 2
Neon Number Sum digits of square

String Programs (~23)

Basics

Learn more: String Basics

Program Recommended Approach
Reverse String StringBuilder.reverse()
Palindrome Check Two pointers or reverse comparison
Compare Strings .equals() / .equalsIgnoreCase()
Count Vowels & Consonants Character classification
Count Characters .length() or loop
Duplicate Characters HashMap frequency
Remove Duplicate Characters LinkedHashSet<Character>
Non-Repeated Characters Frequency count
First Non-Repeated Character LinkedHashMap
Swap Characters Character array manipulation
Digits / Alphabets Only Character.isDigit() / isLetter()

Advanced (Word-Level)

Learn more: Advanced String Programs

Program Recommended Approach
Count Words split("\\s+")
Reverse Each Word Split + StringBuilder.reverse()
Reverse Word Order Reverse words array
Longest / Shortest Word Track maximum and minimum length
Word Frequency HashMap counting
Anagram Check Sort characters or compare frequencies
Sort Words Alphabetically Arrays.sort()
Character Frequency HashMap.getOrDefault()

⭐ = Most Frequently Asked Java Coding Programs


Coding Round Tips

  • Practice writing programs from memory without relying on an IDE.
  • Explain both the brute-force and optimized approaches whenever applicable.
  • Perform a dry run using sample input before finalizing the solution.
  • Always mention time and space complexity.
  • Watch for common mistakes such as integer division, using == instead of .equals() for strings, and inefficient string concatenation inside loops.

Go Deeper

Continue learning with:

  • Complete Java Coding Programs Guide
  • Top 30 SDET Coding Questions
  • Complete Java for Testers Guide
  • List vs Set vs Map