Java Interview Programs
35 Java programs asked in SDET and automation interviews, ordered from easy to hard. Each one has a worked example, hints and starter code; sign in to see the reference solution.
Easy (15)
- Anagram check
Are "listen" and "silent" anagrams?
Easy· Strings ·10 min
- Check for an Armstrong number
Print true if n equals the sum of its digits each raised to the number of digits.
Easy· Numbers ·10 min
- Check whether a number is a palindrome
Print true when the decimal representation reads identically in both directions; negative numbers are not palindromes.
Easy· Numbers ·10 min
- Check whether an integer is prime
Print true if n is prime, otherwise false.
Easy· Numbers ·10 min
- Compute a factorial with BigInteger
Print n factorial as an exact decimal integer.
Easy· Numbers ·10 min
- Count vowels and consonants
Print vowel and consonant counts separated by a space. Count only English letters; vowels are a,e,i,o,u in either case.
Easy· Strings ·10 min
- Find the first occurrence with binary search
Print the zero-based index of the first target occurrence, or -1 if absent.
Easy· Sorting & Searching ·10 min
- Find the GCD and LCM
Print the nonnegative GCD and LCM separated by a space; define both as 0 if both inputs are 0.
Easy· Math ·10 min
- Find the minimum and maximum in an array
Print the minimum and maximum separated by a space.
Easy· Arrays ·10 min
- Generate the Fibonacci series
Print the first n Fibonacci numbers starting with 0 and 1, separated by single spaces; print an empty line for n=0.
Easy· Numbers ·10 min
- Palindrome check ignoring case and spaces
Return true for "Never odd or even".
Easy· Strings ·10 min
- Reverse a String without StringBuilder
Reverse "automation" using a char array. StringBuilder.reverse() is not allowed.
Easy· Strings ·10 min
- Reverse the digits of an integer
Return the reversed integer, preserving its sign and discarding leading zeros.
Easy· Numbers ·10 min
- Reverse words in a sentence
Reverse the order of whitespace-delimited words. Normalize separators to one space and omit leading or trailing whitespace.
Easy· Strings ·10 min
- Sum the digits of an integer
Print the sum of the decimal digits of the absolute value of n.
Easy· Numbers ·10 min
Medium (15)
- Compress consecutive character runs
Replace each maximal run by its character followed by its decimal count, including runs of length one.
Medium· Strings ·20 min
- Find a pair with a target sum
Print indices i j with i<j and a[i]+a[j]=target. Choose the smallest j, then smallest i; print NONE if absent.
Medium· Hashing ·20 min
- Find the distinct intersection of two arrays
Print common distinct values in ascending order, space-separated; print an empty line if the intersection is empty.
Medium· Collections ·20 min
- Find the first non-repeated character
Print the first character occurring exactly once, or NONE if no such character exists.
Medium· Strings ·20 min
- Find the longest common prefix
Print the longest prefix shared by all words, or an empty line if n=0 or no prefix exists.
Medium· Strings ·20 min
- Find the maximum contiguous subarray sum
Print the largest sum of any nonempty contiguous subarray.
Medium· Dynamic Programming ·20 min
- List duplicate characters with frequencies
Print duplicated characters in first-appearance order, each as character:count on its own line. Print NONE if none repeat.
Medium· Hashing ·20 min
- Merge two sorted arrays
Print the merged sorted values, retaining duplicates, separated by single spaces.
Medium· Two Pointers ·20 min
- Missing number in 1..n
An array holds 1..n with exactly one number missing. Find it in O(n) time, O(1) space.
Medium· Arrays ·15 min
- Move zeroes to the end stably
Print the array with all zeroes at the end while preserving nonzero order, separated by single spaces.
Medium· Two Pointers ·20 min
- Remove array duplicates while preserving order
Print distinct values in first-appearance order, space-separated; print an empty line when n=0.
Medium· Collections ·20 min
- Rotate an array to the right
Print the result of rotating right by k positions, space-separated; empty arrays produce an empty line.
Medium· Arrays ·20 min
- Second largest number in an array
Find the second largest distinct value in {12, 45, 45, 3, 78, 9}.
Medium· Arrays ·15 min
- Sort array values by decreasing frequency
Print all values ordered by decreasing occurrence count, breaking ties by ascending numeric value. Retain every occurrence.
Medium· Hashing ·20 min
- Validate balanced brackets
Print true if every closing bracket matches the most recent unmatched opening bracket and no openings remain.
Medium· Stack & Queue ·20 min
Hard (5)
- Detect the entry of a linked-list cycle
Connect the tail to the node at zero-based pos, or leave it null for pos=-1. Print the cycle entry index, or -1 when acyclic; find it with Floyd's algorithm.
Hard· Linked List ·35 min
- Find the longest substring without repeated characters
Print the maximum length of a contiguous substring containing no repeated character.
Hard· Sliding Window ·35 min
- Find the minimum covering substring
Print the shortest source substring containing every target character with its multiplicity. Break ties by earliest start; print an empty line when target is empty or no window exists.
Hard· Sliding Window ·35 min
- Sort an array using merge sort
Print the array in ascending order using merge sort, with values separated by single spaces.
Hard· Sorting & Searching ·35 min
- Traverse a matrix in spiral order
Print a clockwise spiral traversal starting at the top-left corner, space-separated.
Hard· Matrices ·35 min
