How to Use This Guide
Automation Testing and SDET interviews almost always include a coding round focused on arrays, numbers, strings, collections, and basic algorithms.
Each question below provides the recommended approach, expected time complexity where appropriate, and links to the complete working Java implementation.
For the complete collection of approximately 96 Java coding programs, begin with the Complete Java Coding Programs Guide.
Array Questions (Q1–12)
Q1. How do you find the sum and average of array elements?
Accumulate all elements in a loop and divide the total by the array length after casting to double to calculate the average.
Complexity: O(n)
Learn more: Array Basics
Q2. How do you find the largest and smallest element in an array?
Traverse the array once while maintaining the current maximum and minimum values.
Complexity: O(n)
Learn more: Array Basics
Q3. How do you find the second largest element in an array? ⭐
Maintain two variables: largest and secondLargest.
Whenever a new maximum is found, update both values accordingly without sorting.
Complexity: O(n) (No sorting)
Learn more: Array Basics
Q4. How do you find the second smallest element?
Maintain smallest and secondSmallest, initializing them with Integer.MAX_VALUE.
Complexity: O(n)
Learn more: Array Basics
Q5. How do you sort an array using Bubble Sort?
Repeatedly compare adjacent elements and swap them until the largest values move to the end after each pass.
Complexity: O(n²)
Learn more: Sort & Duplicates
Q6. How do you reverse an array?
Use the two-pointer technique by swapping elements from both ends until the pointers meet.
Complexity: O(n) (In-place)
Learn more: Sort & Duplicates
Q7. How do you find duplicate elements in an array? ⭐
Store elements in a HashSet.
If add() returns false, the element is a duplicate.
Complexity: O(n)
Learn more: Sort & Duplicates
Q8. How do you remove duplicates from an array? ⭐
Use a LinkedHashSet to remove duplicate values while preserving insertion order.
Learn more: Sort & Duplicates
Q9. How do you count the frequency of each array element?
Use either:
- A
visited[]array - A
HashMapwithgetOrDefault()
Complexity: O(n)
Learn more: Sort & Duplicates
Q10. How do you find the missing number in an array? ⭐
Calculate the expected sum using:
n × (n + 1) / 2
Subtract the actual array sum.
Complexity: O(n) time, O(1) space
Learn more: Rotation & Patterns
Q11. How do you find a pair with a given sum? ⭐
Store visited elements in a HashSet.
For every element, check whether target - currentElement already exists.
Complexity: O(n)
Learn more: Rotation & Patterns
Q12. How do you find the majority element?
Use Moore's Voting Algorithm with a candidate element and a counter.
Complexity: O(n) time, O(1) space
Learn more: Rotation & Patterns
Number Questions (Q13–21)
Q13. How do you reverse a number? ⭐
Repeatedly extract the last digit using % 10, append it to the reversed number, and remove the last digit using / 10.
Learn more: Number Basics
Q14. How do you check whether a number is a palindrome?
Reverse the number and compare it with the original value.
Learn more: Number Basics
Q15. How do you check whether a number is prime? ⭐
Numbers less than or equal to 1 are not prime.
Check divisibility from 2 up to √n (or n/2).
Learn more: Number Basics
Q16. How do you find the factorial of a number?
Multiply all integers from 1 to the given number using a loop.
Use long for larger values.
Learn more: Number Basics
Q17. How do you check whether a number is an Armstrong number?
Calculate the sum of the cubes of its digits and compare it with the original number.
Example:
153 = 1³ + 5³ + 3³
Learn more: Number Basics
Q18. How do you generate the Fibonacci series? ⭐
Each number is the sum of the previous two values.
The series can be generated using iteration or recursion.
Learn more: Number Basics
Q19. How do you find the GCD and LCM of two numbers?
Use the Euclidean Algorithm to calculate the GCD.
Then compute:
LCM = (a × b) / GCD
Learn more: Advanced Number Programs
Q20. How do you reverse a number using recursion?
Extract one digit, build the reversed number recursively, and stop when the number becomes zero.
Learn more: Advanced Number Programs
Q21. How do you convert a decimal number to binary?
Repeatedly divide the number by 2, store the remainders, and read them in reverse order.
Learn more: Advanced Number Programs
String Questions (Q22–30)
Q22. How do you reverse a string? ⭐
Use:
new StringBuilder(str).reverse().toString()
or iterate through the string in reverse order.
Learn more: String Basics
Q23. How do you check whether a string is a palindrome? ⭐
Compare characters from both ends using two pointers or reverse the string and compare it using .equals().
Learn more: String Basics
Q24. How do you count vowels and consonants?
Convert the string to lowercase and classify each alphabetic character accordingly.
Learn more: String Basics
Q25. How do you find duplicate characters in a string? ⭐
Count character frequencies using a HashMap and display characters whose count exceeds one.
Learn more: String Basics
Q26. How do you remove duplicate characters from a string? ⭐
Use a LinkedHashSet<Character> to preserve the original order while removing duplicates.
Learn more: String Basics
Q27. How do you find the first non-repeated character? ⭐
Use a LinkedHashMap to count character frequencies and return the first character with a frequency of one.
Complexity: O(n)
Learn more: String Basics
Q28. How do you reverse each word in a sentence? ⭐
Split the sentence into words and reverse each word individually using StringBuilder.
Learn more: Advanced String Programs
Q29. How do you count word frequency in a sentence?
Convert the sentence to lowercase, split it into words, and count occurrences using a HashMap.
Complexity: O(n)
Learn more: Advanced String Programs
Q30. How do you check whether two strings are anagrams? ⭐
Normalize both strings, then either:
- Sort and compare the character arrays, or
- Compare character frequency counts.
Learn more: Advanced String Programs
⭐ = Most Frequently Asked Coding Questions
How to Prepare for the Coding Round
- Practice writing programs from memory without relying on an IDE.
- Learn both the brute-force and optimized approaches for common interview questions.
- Explain your thought process while solving the problem.
- Dry-run your solution using sample inputs.
- Always mention the time and space complexity after completing the solution.
Next Steps
Continue your coding interview preparation with:
- Complete Java Coding Programs Guide
- Complete Java for Testers Guide
- SDET Interview Guide