0% found this document useful (0 votes)
8 views6 pages

DSA Questions On Java

The document outlines common array, string, linked list, stack, queue, hashing, map-based, tree, recursion, and basic dynamic programming questions frequently asked in Java interviews. Each question includes follow-up inquiries that explore deeper understanding and alternative approaches. It emphasizes the importance of practicing solutions in a structured manner, covering brute force ideas, optimized approaches, and complexities.

Uploaded by

naseem
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

DSA Questions On Java

The document outlines common array, string, linked list, stack, queue, hashing, map-based, tree, recursion, and basic dynamic programming questions frequently asked in Java interviews. Each question includes follow-up inquiries that explore deeper understanding and alternative approaches. It emphasizes the importance of practicing solutions in a structured manner, covering brute force ideas, optimized approaches, and complexities.

Uploaded by

naseem
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Array & String Questions

(Asked in almost every Java interview)

Q1. Given an integer array, how will you find the second-largest element without
sorting the array?​
Follow-up questions:

●​ What if the array contains duplicates?


●​ What if all elements are negative?
●​ Can you do this in a single traversal?

Q2. You are given an array and a number K. Rotate the array by K positions.​
Follow-up questions:

●​ Rotate left vs rotate right


●​ Solve it in-place
●​ What is the time and space complexity of each approach?

Q3. An array contains numbers from 1 to N, but one number is missing. How will you
find it?​
Follow-up questions:

●​ Use XOR instead of sum


●​ Which approach is safer for large values and why?
●​ What if two numbers are missing?

Q4. Find all duplicate elements in an array.​


Follow-up questions:

●​ With extra space


●​ Without extra space
●​ What if the numbers are from 1 to N only?

[Link] a string, find the length of the longest substring without repeating
characters.​
Follow-up questions:
●​ Explain the sliding window approach
●​ Why HashMap instead of HashSet?
●​ How does this perform for very large strings?

Linked List Questions


(Core logic + pointer manipulation)

Q6. Reverse a singly linked list.​


Follow-up questions:

●​ Iterative vs recursive
●​ Which one is better and why?
●​ What happens to space complexity in recursion?

Q7. How will you detect a loop in a linked list?​


Follow-up questions:

●​ Explain Floyd’s Cycle Detection Algorithm


●​ Can you find the starting point of the loop?
●​ What is the real-world use case of this problem?

Q8. Find the middle element of a linked list in a single traversal.​


Follow-up questions:

●​ Why does the slow-fast pointer approach work?


●​ What happens if the list length is even?

Q9. Merge two sorted linked lists into one sorted list.​
Follow-up questions:

●​ Iterative vs recursive solution


●​ Time and space complexity
●​ Where do we use this in real applications?
Q10.

Remove the Nth node from the end of a linked list.​


Follow-up questions:

●​ Can you do it in one pass?


●​ How will you handle edge cases like N equals list length?

Stack & Queue Questions


(Data structure behavior matters here)

Q11. Check whether a given string has balanced parentheses.​


Follow-up questions:

●​ Extend it to {}, [], ()


●​ Why stack is the best choice here?

Q12. Implement a stack using an array.​


Follow-up questions:

●​ How do you handle stack overflow and underflow?


●​ Implement stack using a linked list
●​ Compare both approaches

Q13. Find the next greater element for each element in an array.​
Follow-up questions:

●​ Brute force vs optimized approach


●​ Why stack works here?
●​ Time complexity analysis
Q14. Implement a queue using two stacks.​
Follow-up questions:

●​ Enqueue costly vs dequeue costly approach


●​ Which approach is better and why?

Q15. Given a stream of integers, find the maximum element at any point.​
Follow-up questions:

●​ Which data structure will you use?


●​ How will you optimize it?

Hashing & Map-Based Questions


(Most common for backend Java roles)

Q16. Given an array, find if there exists a pair with a given sum.​
Follow-up questions:

●​ Using HashSet
●​ If the array is sorted
●​ What changes if duplicates are allowed?

Q17. Find the first non-repeating character in a string.​


Follow-up questions:

●​ Why LinkedHashMap is useful in Java?


●​ Time and space complexity

Q18. Group all anagrams together from a list of strings.​


Follow-up questions:

●​ How do you generate the key for HashMap?


●​ Can this be optimized further?
Q19. Count the frequency of elements in an array efficiently.​
Follow-up questions:

●​ Which Java collection will you use and why?


●​ How does this scale for large inputs?

Q20. Find the longest consecutive sequence in an unsorted array.​


Follow-up questions:

●​ Why hashing is better than sorting here?


●​ Time complexity comparison

Tree, Recursion & Basic DP Questions


(Asked in mid-level interviews)

Q21. Write recursive code for inorder, preorder, and postorder traversal of a binary tree.​
Follow-up questions:

●​ Time and space complexity


●​ Can you do it iteratively?

Q22. Find the height of a binary tree.​


Follow-up questions:

●​ What is the base case?


●​ How does recursion stack affect space?

Q23. How will you check if a binary tree is a Binary Search Tree?​
Follow-up questions:

●​ Why naive approach fails


●​ Explain range-based validation
Q24. Find the Lowest Common Ancestor of two nodes in a binary tree.​
Follow-up questions:

●​ Difference between BT and BST approach


●​ Time complexity analysis

Q25. Explain Fibonacci using recursion and then optimize it.​


Follow-up questions:

●​ Why recursion is inefficient here


●​ How does dynamic programming help?
●​ Time complexity before and after optimization

Tip: For each question, practice answering in this order


1.​ Brute force idea
2.​ Optimized approach
3.​ Java code
4.​ Time and space complexity
5.​ Edge cases
6.​ Real-world analogy

You might also like