Java Coding Interview Questions for Freshers (2026): 25 Programs with Logic, Code, Output and Complexity

Which Java programs are asked in fresher interviews? The most common are reverse a string, palindrome, Fibonacci, prime number, factorial, Armstrong number, anagram check, count vowels, find the largest and second largest in an array, remove duplicates, swap two numbers without a third variable, word frequency with HashMap and star or number patterns. Interviewers care less about the answer and more about whether you can explain your logic, handle edge cases and state the time complexity.
This guide from VibrantMinds Technologies in Pune gives 25 Java coding interview questions for freshers in a fixed format: problem, logic, code, sample output and complexity. It is written for anyone searching Java coding interview questions for freshers, Java programs for interview, Java coding questions with solutions, Java string programs for interview, Java array programs for interview, Java pattern programs for freshers, Java coding round for freshers, Java logical programs for interview or a free IT job WhatsApp group for Java freshers in Pune.
Takeaway | Detail |
|---|---|
Explain before you code | State your approach in 2 to 3 lines first. |
Master about 25 patterns | Most fresher questions are variations of these. |
Mention complexity | Say “O(n) time, O(1) space” without being asked. |
Test with edge cases | Empty input, single element, negatives, duplicates. |
Know the built-in way and the manual way | Interviewers often ask for no built-in methods. |
Type the code yourself | Reading solutions isn’t practice. |
A 5-STEP METHOD TO ANSWER ANY CODING QUESTION
CATEGORY A: NUMBER PROGRAMS (1 TO 7)
Program 1: Reverse a number
Logic: repeatedly take the last digit (n % 10), build the reverse, then drop the digit (n / 10).
java
Complexity: O(d) time where d is the number of digits, O(1) space.
Program 2: Palindrome number
Logic: reverse the number and compare with the original.
java
Complexity: O(d), O(1).
Program 3: Factorial
Logic: multiply 1 to n. Use long since factorials grow quickly.
java
Complexity: O(n), O(1). Mention: very large n overflows long, so BigInteger is needed.
Program 4: Fibonacci series
Logic: each number is the sum of the previous two.
java
Complexity: O(n), O(1). Follow-up: the simple recursive version is O(2^n), so mention memoization.
Program 5: Prime number check
Logic: a number is prime if no number from 2 to its square root divides it.
java
Complexity: O(√n), O(1).
Program 6: Armstrong number
Logic: the sum of each digit raised to the power of the digit count equals the number (153 = 1³ + 5³ + 3³).
java
Complexity: O(d), O(1).
Program 7: Swap two numbers without a third variable
Logic: use arithmetic.
java
Mention: readability matters, and a temp variable is perfectly fine in real code. Large values can overflow with addition.
CATEGORY B: STRING PROGRAMS (8 TO 13)
Program 8: Reverse a string (without built-in reverse)
Logic: loop from the end and append each character
java
Better: new StringBuilder(s).reverse().toString(). Say why: += in a loop creates many String objects, as Strings are immutable. Complexity is O(n) with StringBuilder.
Program 9: Palindrome string
Logic: two pointers moving inward.
java
Complexity: O(n), O(1). Edge case: ask whether case and spaces matter.
Program 10: Count vowels and consonants
java
Complexity: O(n), O(1).
Program 11: Check if two strings are anagrams
Logic: two strings are anagrams if sorted characters match.
java
Complexity: O(n log n). Follow-up: a frequency array of size 26 gives O(n).
Program 12: First non-repeating character
Logic: count frequencies in a LinkedHashMap, then return the first with count 1.
java
Complexity: O(n), O(k) for k unique characters.
Program 13: Remove duplicate characters from a string
java
Complexity: O(n), O(k).
CATEGORY C: ARRAY PROGRAMS (14 TO 19)
Program 14: Find the largest element
java
Complexity: O(n), O(1).
Program 15: Find the second largest element
Logic: one pass, tracking the largest and second largest without sorting.
java
Complexity: O(n), O(1). Edge case: arrays with all equal elements have no second largest.
Program 16: Reverse an array in place
java
Complexity: O(n), O(1).
Program 17: Remove duplicates from a sorted array (in place)
java
Complexity: O(n), O(1).
Program 18: Find the missing number from 1 to n
Logic: expected sum is n(n+1)/2, so subtract the actual sum.
java
Complexity: O(n), O(1).
Program 19: Two sum (find a pair with a given sum)
Logic: store seen numbers in a HashMap and look for the complement.
java
Complexity: O(n), O(n). The brute-force nested loop is O(n²), so mention the improvement.
CATEGORY D: PATTERN PROGRAMS (20 TO 22)
Program 20: Right-angled star triangle
java
Program 21: Pyramid
java
Program 22: Number triangle
java
Pattern tip: the outer loop controls rows, the inner loops control spaces and characters. Complexity is O(n²).
CATEGORY E: COLLECTIONS-BASED PROGRAMS (23 TO 25)
Program 23: Word frequency count
java
Complexity: O(n), O(k).
Program 24: Find duplicates in a list
java
Complexity: O(n), O(n). add() returns false if the element already exists.
Program 25: Sort a list of objects by a field
java
Complexity: O(n log n). Mention: Comparable for natural order and Comparator for custom order.
COMPLEXITY CHEAT SHEET
Complexity | Meaning | Example from this guide |
|---|---|---|
O(1) | Constant | Accessing an array element |
O(log n) | Halves each step | Binary search |
O(n) | One pass | Find max, two sum with HashMap |
O(n log n) | Sorting | Anagram by sorting |
O(n²) | Nested loops | Brute-force two sum, patterns |
O(√n) | Up to square root | Prime check |
EDGE CASES CHECKLIST (TEST EVERY SOLUTION)
Input type | Edge cases to try |
|---|---|
Numbers | 0, 1, negative, very large value, single digit |
Strings | empty, one character, all same, mixed case, spaces, special characters |
Arrays | empty, one element, all equal, already sorted, negatives, duplicates |
Collections | empty list, null values, duplicate keys |
MISTAKES FRESHERS MAKE IN THE JAVA CODING ROUND
Mistake | Why it hurts | Better approach |
|---|---|---|
Coding in silence | Interviewer can’t follow your thinking | Narrate your approach |
Jumping straight to code | Logic errors | Work an example first |
Ignoring edge cases | Code fails on empty or negative input | Use the checklist |
Using | Inefficient | Use StringBuilder |
Memorised solutions | Can’t adapt to a twist | Understand the logic |
Not stating complexity | Misses easy credit | End each answer with it |
Giving up on the optimal answer | Leaves a weaker impression | Offer brute force first, then improve |
Messy variable names | Hard to read | Use clear names |
14-DAY JAVA CODING PRACTICE PLAN
Day | Focus | Programs |
|---|---|---|
1 | Warm-up and method | Learn the 5-step method. Programs 1 and 2. |
2 | Numbers | Programs 3 to 5 |
3 | Numbers | Programs 6 and 7, plus a new variation |
4 | Strings 1 | Programs 8 to 10 |
5 | Strings 2 | Programs 11 to 13 |
6 | Revision | Redo Programs 1 to 13 without looking |
7 | Arrays 1 | Programs 14 to 16 |
8 | Arrays 2 | Programs 17 to 19 |
9 | Patterns | Programs 20 to 22, then invent 2 new patterns |
10 | HashMap and Set | Programs 23 and 24 |
11 | Sorting objects | Program 25, plus Comparable vs Comparator |
12 | Timed practice | 3 random programs in 30 minutes |
13 | Mock round | Solve 4 programs aloud, stating complexity |
14 | Final polish | Review mistakes, redo weak programs, explain your project |
MYTH VS REALITY
Myth | Reality |
|---|---|
“I must memorise 100 programs.” | Learn about 25 patterns and understand them. |
“Built-in methods are always accepted.” | Many interviewers ask you to solve it manually first. |
“Only the correct output matters.” | Explanation, edge cases and clean code are judged too. |
“Coding rounds need advanced DSA for every fresher job.” | Many service-company rounds test these basics. Requirements vary by company. |
“If I get stuck, it’s over.” | Explaining your thinking and offering a brute-force solution can still help. |
HOW VIBRANTMINDS HELPS FRESHERS CRACK JAVA CODING ROUNDS
VibrantMinds Technologies Pvt. Ltd. is a Pune-based IT training and placement support company. If you searched for VibrantMinds or VibrantMinds Technologies, here is what it offers:
Full Stack Java Development (about 4 to 6 months): Core Java, OOP, collections, JDBC, SQL, Spring Boot, REST APIs, front-end basics, projects, aptitude and interview preparation. Please check the candidate portal for the latest syllabus and tools.
Core Java course (about 1 to 2 months) for strong fundamentals
Software Testing (Manual + Automation) and Python & Data Science for other career paths
Interview preparation: Coding practice, technical rounds, project rounds, HR rounds, aptitude, communication and mock interviews
Placement support: Screening, drive coordination and interview support. Check https://candidates.vibrantminds.in/ for current openings.
Free WhatsApp job alert groups for fresher IT jobs and drives
Honesty first: VibrantMinds offers placement support, not a guaranteed job, and no legitimate recruiter should ask you to pay to be interviewed.
VibrantMinds works as a talent ecosystem connecting candidates, colleges and companies, with 1900+ partner colleges and 600+ hiring companies. More fresher job guidance: https://candidates.vibrantminds.in/blog/fresher-jobs-pune-2026
VIBRANTMINDS COURSES AT A GLANCE
Course | Duration | Key topics |
|---|---|---|
Full Stack Java Development | 4 to 6 months | Core Java, OOP, JDBC, SQL, Spring Boot, REST APIs, front-end basics, projects, aptitude and interview preparation |
Software Testing (Manual + Automation) | 2 to 5 months | Test cases, STLC, bug tracking, SQL, Selenium basics |
Core Java | 1 to 2 months | Syntax, OOP, collections, exceptions, strings |
Python & Data Science | 2 to 4 months | Python basics, data handling, analysis fundamentals |
Fees, syllabus and batch dates are on https://candidates.vibrantminds.in/. Please confirm them directly before enrolling.
THE VIBRANTMINDS PLACEMENT PIPELINE: SIX STEPS
REAL OUTCOMES: WHAT TO EXPECT AND WHAT NOT TO
Past students have shared positive feedback about the training and support, and have gone on to roles at companies such as Aress Software and Education Technologies, Saama Technologies and Three M Software. Results vary by candidate, skill level, effort and market conditions, so please confirm details directly with the VibrantMinds team. No one can honestly promise a guaranteed job.
JOIN FREE VIBRANTMINDS WHATSAPP GROUPS: JAVA AND IT JOB UPDATES FOR FRESHERS IN PUNE AND INDIA
Looking for free WhatsApp groups, free IT job WhatsApp groups, a Java WhatsApp group for freshers or a job updates WhatsApp group in India? The VibrantMinds groups share fresher IT openings, campus drives, walk-in interview alerts and interview preparation updates, free of charge. Join whichever group has space. All three carry the same type of updates.
Group | What it offers | Link |
|---|---|---|
VibrantMinds Fresher IT Jobs, Group 1 | Fresher openings, campus drives, walk-in alerts | |
VibrantMinds Fresher IT Jobs, Group 2 | Fresher openings, campus drives, walk-in alerts | |
VibrantMinds Fresher IT Jobs, Group 3 | Fresher openings, campus drives, walk-in alerts |
Never pay to apply. Legitimate recruiters do not charge candidates for tests, interviews or offer letters. Do not share sensitive personal or financial details in any group chat.
FREQUENTLY ASKED QUESTIONS
1. What are the most asked Java coding interview questions for freshers?
Reverse a string, palindrome, Fibonacci, prime number, factorial, Armstrong number, anagram check, second largest in an array, remove duplicates, missing number, two sum, word frequency using HashMap and star or number patterns.
2. How do I reverse a string in Java without using a built-in method?
Loop from the last index to the first and append each character to a result, or swap characters from both ends using two pointers on a char[]. For efficiency, build the result with a StringBuilder instead of +=, because Strings are immutable.
3. How do I find the second largest number in an array in Java?
Track two variables, first and second, in a single pass. Update second to the old first when a bigger number appears, and update second when a number lies between them. This is O(n) and needs no sorting.
4. How do I check if two strings are anagrams in Java?
Check that the lengths match, then either sort both character arrays and compare them (O(n log n)), or count character frequencies in an array of size 26 (O(n)).
5. Is there a free WhatsApp group for Java freshers in Pune?
Yes. VibrantMinds runs free fresher IT job WhatsApp groups that share Java and other IT openings, campus drives and walk-in alerts. Join through the links above, and always verify a job post and never pay to apply.
CONCLUSION: PRACTISE THE PATTERNS, EXPLAIN THE LOGIC
The best way to handle Java coding interview questions for freshers is to learn the core 25 patterns, type each program yourself, say your logic aloud, mention complexity and test edge cases. Follow the 14-day plan, then try small variations so you aren’t relying on memory alone.
If you’re in Pune and want structured Java training with coding practice, interview preparation and placement support, VibrantMinds Technologies is here to help.
Call to action:
Website: https://vibrantminds.in/
Candidate portal and courses: https://candidates.vibrantminds.in/
Fresher jobs guide: https://candidates.vibrantminds.in/blog/fresher-jobs-pune-2026
Join the free WhatsApp groups: the three links above
VISIT OR CONTACT VIBRANTMINDS IN PUNE
Company: VibrantMinds Technologies Pvt. Ltd.
Address: Viva Academy Building, Warje, Pune, Maharashtra 411052
Phone: +91 95035 79517
Email: support@vibrantmindstech.com
Website: https://vibrantminds.in/
Candidate portal and courses: https://candidates.vibrantminds.in/


