Table of Contents
DSA & Java — Practice Questions (Easy + Medium)
No Answers | All Topics Covered
Topic 1: Arrays & Strings
Easy
1. Write a Java program to find the largest element in an array without using
[Link]().
2. Write a Java program to reverse a string without using
[Link]().
3. Given an array of integers, count how many elements are even and how many are
odd.
4. Write a Java program to check if two strings are anagrams of each other.
5. Find the sum of all elements in an integer array.
Medium
6. Write a Java method to rotate an array to the right by k positions (without using
extra array).
7. Given a string, find the first non-repeating character and return its index. Return -1
if none exists.
8. Write a Java method to find all pairs in an array that sum to a given target value k.
9. Given a sorted array, remove duplicates in-place and return the new length.
10. Write a program to find the longest common prefix among an array of strings.
Topic 2: Linked Lists
Easy
1. Write a Java method to insert a node at the end of a singly linked list.
2. Write a Java method to count the number of nodes in a linked list.
3. Write a Java method to search for a value in a linked list and return true if found.
4. Write a Java method to delete the first node of a linked list.
5. Print all elements of a linked list in reverse order (without reversing the list).
Medium
6. Write a Java method to find the nth node from the end of a linked list in a single
pass.
7. Write a Java method to merge two sorted linked lists into one sorted linked list.
8. Detect if a linked list has a cycle using Floyd’s algorithm.
9. Write a Java method to remove all nodes with a given value from a linked list.
10. Given a linked list, check if it is a palindrome.
Topic 3: Stacks & Queues
Easy
1. Implement a Stack using an array in Java with push, pop, peek, and isEmpty
methods.
2. Using Java’s built-in Stack, reverse a string using push and pop.
3. Write a program to check if a given string of brackets ()[]{} is balanced.
4. Implement a Queue using an array in Java with enqueue and dequeue operations.
5. Write a program that evaluates how many elements are currently in a stack after a
series of push/pop operations.
Medium
6. Implement a Queue using two Stacks in Java.
7. Design a Stack that supports push, pop, and getMin — all in O(1) time.
8. Given a sequence of push and pop operations, determine if the pop sequence is
valid.
9. Write a Java method to sort a stack in ascending order using only one additional
stack.
10. Implement a circular queue using an array in Java.
Topic 4: Recursion
Easy
1. Write a recursive Java method to compute the factorial of a number n.
2. Write a recursive Java method to compute the nth Fibonacci number.
3. Write a recursive Java method to compute the sum of digits of a number.
4. Write a recursive method to print numbers from 1 to n.
5. Write a recursive method to check whether a string is a palindrome.
Medium
6. Write a recursive Java method to compute base^exp in O(log n) time (fast
exponentiation).
7. Solve the Tower of Hanoi problem for n disks and print each move.
8. Write a recursive method to find all subsets (power set) of a given array.
9. Write a recursive Java method to count the number of ways to climb n stairs, taking
1 or 2 steps at a time.
10. Given a string, generate all permutations of its characters using recursion.
Topic 5: Sorting & Searching
Easy
1. Implement Bubble Sort in Java and sort an array in ascending order.
2. Implement Selection Sort in Java.
3. Write a Java method to perform linear search on an unsorted array.
4. Write a Java method to perform binary search on a sorted array (iterative).
5. Given two sorted arrays, merge them into a single sorted array.
Medium
6. Implement Merge Sort in Java to sort an array in descending order.
7. Implement Quick Sort in Java and identify its worst-case scenario.
8. Write a binary search method that returns the index of the first occurrence of a
target in a sorted array with duplicates.
9. Given an array, find the kth smallest element using sorting.
10. Write a Java method to sort an array of 0s, 1s, and 2s without using any sorting
library (Dutch National Flag problem).
Topic 6: Trees & Graphs
Easy
1. Write a Java method to perform inorder traversal of a binary tree.
2. Write a Java method to perform preorder traversal of a binary tree.
3. Write a Java method to find the height of a binary tree.
4. Write a Java method to count the total number of nodes in a binary tree.
5. Write a Java method to check if a value exists in a Binary Search Tree (BST).
Medium
6. Write a Java method to perform level-order (BFS) traversal of a binary tree and
print each level on a new line.
7. Write a Java method to find the lowest common ancestor (LCA) of two nodes in a
BST.
8. Write a Java method to check whether a given binary tree is a valid BST.
9. Implement DFS on a graph represented as an adjacency list.
10. Write a Java method to detect a cycle in an undirected graph using BFS.
Topic 7: Dynamic Programming
Easy
1. Write a Java program to compute the nth Fibonacci number using tabulation
(bottom-up DP).
2. Given a set of coin denominations and a target amount, find the minimum number of
coins needed (Coin Change problem).
3. Write a Java method to find the length of the longest increasing subsequence (LIS) in
an array.
4. Given two strings, find the length of their Longest Common Subsequence (LCS).
5. Write a Java program to solve the 0/1 Knapsack problem using a 2D DP table.
Medium
6. Write a Java method to find the number of unique paths in an m x n grid from top-
left to bottom-right (only right and down moves allowed).
7. Given a string, find the length of the longest palindromic subsequence.
8. Write a Java program to solve the subset sum problem — determine if any subset of
a given array sums to a target S.
9. Given a matrix of integers, find the maximum sum submatrix.
10. Write a Java method to find the minimum number of edits (insertions, deletions,
substitutions) to convert one string into another (Edit Distance).
Topic 8: Java References (Pointers & Memory)
Easy
1. What is the output when a primitive int is passed to a method and modified inside
— does the original change?
2. Write a Java program to demonstrate that modifying an array inside a method
affects the original array.
3. Explain the difference between == and .equals() with a String example.
4. Write a Java program that creates an object, passes it to a method, modifies a field,
and prints the field from main.
5. Write a program to demonstrate what happens when you reassign an object
reference inside a method.
Medium
6. Write a Java method that takes an array and doubles every element in place —
explain why this works with references.
7. Given a class Node with int val and Node next, write a method to deep copy a
linked list.
8. Write a Java program to show that String is immutable — modifications inside a
method do not affect the original.
9. Demonstrate shallow copy vs deep copy using a class that contains an array field.
10. Write a method that swaps two integers using a wrapper array — explain why
direct swap with primitives doesn’t work.
Topic 9: Java Classes & Objects (Structs)
Easy
1. Create a Student class with fields name, age, and gpa. Write a constructor and a
method to display details.
2. Create a Rectangle class with length and width. Add methods to compute area
and perimeter.
3. Write a Java class BankAccount with deposit and withdraw methods, and a balance
field.
4. Demonstrate constructor overloading with a Book class that has different
constructors.
5. Write a class Point with x and y coordinates and a method distanceTo(Point
other).
Medium
6. Create a Stack class using an array that supports push, pop, peek, and isEmpty
with overflow/underflow handling.
7. Write a LinkedList class with its own Node inner class and methods:
insertFront, deleteFront, print.
8. Implement a Queue class using two Stack objects (composition).
9. Create a BinarySearchTree class with insert and inorder methods.
10. Write a Student class that implements Comparable<Student> to compare
students by GPA, and sort a list of students.
Topic 10: File I/O in Java
Easy
1. Write a Java program to create a new file and write the text "Hello, World!" to it
using FileWriter.
2. Write a Java program to read and print all lines from a text file using
BufferedReader.
3. Write a Java program to append a line to an existing file without overwriting its
content.
4. Write a Java program to count the total number of lines in a text file.
5. Write a Java program to check if a file exists before reading it.
Medium
6. Write a Java program to copy the contents of one file into another file line by line.
7. Write a Java program to count the frequency of each word in a text file and print the
results.
8. Write a Java program to read a CSV file line by line and store each row as a
String[] in a list.
9. Write a Java program to write an array of Student objects to a file, one per line in
the format name,age,gpa.
10. Write a Java program to search for a specific word in a file and print the line
numbers where it appears.
Good luck! 💪