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)

  1. Anagram check

    Are "listen" and "silent" anagrams?

    Easy· Strings ·10 min

  2. 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

  3. 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

  4. Check whether an integer is prime

    Print true if n is prime, otherwise false.

    Easy· Numbers ·10 min

  5. Compute a factorial with BigInteger

    Print n factorial as an exact decimal integer.

    Easy· Numbers ·10 min

  6. 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

  7. 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

  8. 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

  9. Find the minimum and maximum in an array

    Print the minimum and maximum separated by a space.

    Easy· Arrays ·10 min

  10. 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

  11. Palindrome check ignoring case and spaces

    Return true for "Never odd or even".

    Easy· Strings ·10 min

  12. Reverse a String without StringBuilder

    Reverse "automation" using a char array. StringBuilder.reverse() is not allowed.

    Easy· Strings ·10 min

  13. Reverse the digits of an integer

    Return the reversed integer, preserving its sign and discarding leading zeros.

    Easy· Numbers ·10 min

  14. 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

  15. 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)

  1. 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

  2. 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

  3. 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

  4. Find the first non-repeated character

    Print the first character occurring exactly once, or NONE if no such character exists.

    Medium· Strings ·20 min

  5. 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

  6. Find the maximum contiguous subarray sum

    Print the largest sum of any nonempty contiguous subarray.

    Medium· Dynamic Programming ·20 min

  7. 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

  8. Merge two sorted arrays

    Print the merged sorted values, retaining duplicates, separated by single spaces.

    Medium· Two Pointers ·20 min

  9. 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

  10. 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

  11. 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

  12. 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

  13. Second largest number in an array

    Find the second largest distinct value in {12, 45, 45, 3, 78, 9}.

    Medium· Arrays ·15 min

  14. 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

  15. 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)

  1. 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

  2. Find the longest substring without repeated characters

    Print the maximum length of a contiguous substring containing no repeated character.

    Hard· Sliding Window ·35 min

  3. 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

  4. 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

  5. Traverse a matrix in spiral order

    Print a clockwise spiral traversal starting at the top-left corner, space-separated.

    Hard· Matrices ·35 min