Java coding programs for interviews

Why These Programs?

Automation interviews almost always include a coding round — reverse a string, remove duplicates, find the second largest, pair with a given sum. This guide collects ~96 such programs, grouped by data type, each with working Java code and its logic explained. It directly answers the coding-round questions that companies like Wipro, Cognizant, and Coforge ask.


Part 1 — Array Programs

Part 2 — Number Programs

Part 3 — String Programs

  • String Basics — reverse, palindrome, compare, vowels/consonants, duplicate & first-non-repeated characters.
  • String Advanced (Word-Level) — reverse each word, word order, longest word, word frequency, anagram, sort words.

How to Use This Guide

  • Interview prep: work through all three parts and practice writing each from memory.
  • By topic: jump to arrays, numbers, or strings depending on what you're weak on.
  • Company-specific: these answer the coding rounds flagged in the Company Interview guides (reverse string, remove duplicates, second largest, pair sum).

Pair with the Complete Java-for-Testers Guide for the concepts behind the code.

Frequently Asked Questions

What coding questions are commonly asked in automation testing interviews?

Automation testing interviews commonly include coding problems based on arrays, numbers, and strings, such as:

Advertisement
  • Reverse an array or string
  • Find duplicate elements
  • Find the second largest element
  • Pair with a given sum
  • Prime number
  • Palindrome
  • Anagram

These programs help assess problem-solving skills and Core Java knowledge required for SDET and automation roles.


How do you find the second largest element in an array?

The most efficient approach is to traverse the array once while maintaining the largest and second-largest values.

This solution works in O(n) time without sorting the array and is covered in the Array Basics tutorial.


How do you find duplicate elements efficiently?

A HashSet provides an efficient way to detect duplicate elements.

If add() returns false, the element already exists in the set, making the solution run in O(n) time instead of O(n²).

This approach is explained in the duplicate-elements program tutorial.


How do you check whether two strings are anagrams?

Anagrams can be verified by:

  • Sorting both strings and comparing them, or

  • Comparing the frequency of each character.

Both approaches are covered in the Advanced String Programs tutorial.


Do these coding programs include runnable Java code?

Yes.

Every coding program includes complete Java source code that you can copy, compile, and run directly for practice and interview preparation.