This is a coding question recently asked to one of my readers in a Java Technical interview on investment bank. The question was to remove duplicates from an integer array without using any collection API classes like Set, HashSet, TreeSet or LinkedHashSet, which can make this task trivial. In general, if you need to do this for any project work, I suggest better using the Set interface, particularly LinkedHashSet, because that also keeps the order on which elements are inserted into Set. Why Set? because it doesn't allow duplicates and if you insert duplicate the add() method of Set interface will return false.
Thursday, July 3, 2025
How to find smallest number from int array in Java, Python, JavaScript and Golang? [Solved]
This is the second part on this array interview question. In the first part, I have showed you how to find the largest element in array, and in this part, you will learn how to find the smallest number from an integer array. We will solve this problem on Java programming language but you are free to solve in any other programming language like Python or JavaScript. Most interviewer doesn't care much about which programming language you are solving the question, if you can provide working solution. This one is also a popular Java programming exercise which is taught in school and colleges and give in homework to teach programming to kids and engineering graduates.
Labels:
Array
,
Coding problems
,
data structure and algorithm
How to Find Multiple Missing Integers in Given Array of Numbers with Duplicates in Java?
Hello guys, It's been a long time since I have discussed any coding or algorithm interview questions, so I thought to revisit one of the most popular array-based coding problems of finding missing numbers in a given array of integers. You might have heard or seen this problem before on your programming job interviews and you might already know how to solve this problem. But, there are a lot of different versions of this problem with increasing difficulty levels which interviewers normally use to confuse candidates and further test their ability to adapt to frequent changes, which is key to surviving in the ever-changing software development world.
Difference between a List and Array in Java? ArrayList vs Array Example
Hello there, if you are wondering what is the difference between a list and an array, or particularly an ArrayList and an Array in Java then you have come to the right place. Earlier, I have shared the best data structure and algorithm courses and in this article, I will explain to you the differences and similarities between a list and an array in Java. Both array and ArrayList are two important data structures in Java and frequently used in Java programs. Even though ArrayList is internally backed by an array, knowing the difference between an array and an ArrayList in Java is critical for becoming a good Java developer. If you know the similarity and differences, you can judiciously decide when to use an array over an ArrayList or vice-versa.
How to Find all Pairs in Array of Integers Whose sum is Equal to a Given Number in Java? Solution Example
Practicing coding problems are very important to do well in any programming interview. You should at your best on data structures like an array, linked list, and string to clear any programming interview and believe me, you can not do this in one day or one week. It's rather a long process of learning through coding, and that's where these small coding problems help. Today, we are going to look at another interesting programming question from the array; write a program to find all pairs of integers whose sum is equal to a given number. For example, if the input integer array is {2, 6, 3, 9, 11} and the given sum is 9, the output should be {6,3}.
Labels:
Array
,
core java
,
data structure and algorithm
,
interview questions
,
programming
How to Create and Initialize Anonymous Array in Java? Example
Anonymous arrays in Java is an Array without any name, just like Anonymous inner classes and policy of using Anonymous array is just create, initialize and use it, Since it doesn't have any name you can not reuse it. The anonymous array was a good way to implement variable argument methods before Java introduced varargs in Java5. You had the liberty to create an array of any length and pass that to a method that operates on an anonymous array. Classical example of such variable argument method is aggregate function like sum(), avg(), min(), max() etc.
Labels:
Array
,
core java
,
data structure and algorithm
Can You Make an Array or ArrayList Volatile in Java?
This is one of the many interesting multi-threading questions I have shared in my post 50 multi-threading interview questions. Yes, you can make an array volatile in Java, there is no problem with that, neither compiler will flag any error not JVM will throw any exception but the tricky part is why you want to make an array volatile and what is the effect of making an array volatile in Java? In order to answer this question, you must be familiar with both volatile modifier and Java memory model, otherwise, it would be difficult to answer, and that's why it's also one of the trick questions from Java interviews. Before answering this question in detail, let's first revise what is a volatile keyword in Java and what kind of guarantee it provides in the context of multithreading and concurrency.
Labels:
Array
,
Java multithreading Tutorials
Difference between Linked List and Array in Java? Example
Array and linked lists are two fundamental data structures in the programming world. Almost all programs use Array in some form or other, which makes it increasingly important to learn array and linked list. The difference between the linked list and array data structure is also a popular data structure question, frequently asked in the various programming job interviews. This makes it even more important to learn and understand the difference between an array and a linked list. Well, there are a lot of differences between these two starting from how they store data, to how you retrieve data from them.
Labels:
Array
,
coding
,
core java
,
core java interview question
,
data structure and algorithm
,
programming
How to merge two sorted arrays in Java? Example Tutorial
Hello guys, if you want to learn how to merge two sorted arrays in Java then you have come to the right place. Earlier, I have shown you how to sort arrays in Java, and in this article, I will show you how to merge two sorted arrays in Java. Btw, if you are a complete beginner then Firstly, you need to know what an array is before going about solving this problem about sorted array. An array is a data structure in Java that holds values of the same type with its length specified right from creation time. This means you can create an integer array like int[] to store integer value. Think of a container, like a crate of eggs or coke, where number of places are fixed and you cannot change once you created it.
Labels:
Array
,
Coding problems
,
core java
How to check if a given linked list is a palindrome in Java? Example [Solved]
Hello guys, if you are wondering how to check if a given linked list is a
palindrome in Java then you have come to the right place. In the past, I
have shared how to check if a given String is palindrome or a given number is a palindrome, and in this article, I will teach you how to check if a linked list is a
palindrome. But before that, let's revise what is a palindrome? A palindrome
is a phrase, word or number, or other sequence of characters that reads the
same backward ad forward. Meaning that It gives you the same thing when read
forward or backward. For example, Mary, Army, Madam, racecar, 1-2-3-2-1,
noon, level, Hannah, civic, etc.
Labels:
Coding problems
,
data structure and algorithm
,
linked list
[Solved] How to reverse a Stack in Java using Recursion and Iteration? Example Tutorial
Hello guys, if you are wondering how to reveres a stack in Java then don't go
anywhere. In this article, I will show you step by step
how to reverse a given stack in Java. There are two ways to reverse a stack, you can use
iteration or
recursion. The most common way of reversing a stack is to use an auxiliary stack. First,
we will pop all the elements from the stack and push them into the auxiliary
stack. Once all the elements are pushed into the auxiliary stack, then it
contains the elements in the reverse order and we simply print them. But, here,
we will not use the auxiliary stack. We will use a recursion method to reverse a
stack where
recursion
means calling the function itself again and again.
Labels:
Coding problems
,
data structure and algorithm
,
stack
[Solved] How to Implement Fibonacci Series with Memoization in Java 8? ConcurrentHashMap Caching Example
Hello guys, if you are wondering how to solve the Fibonacci series problem with memorization in Java then you have come to the right place. In the past, I have shared recursive and iterative Fibonacci series solution and in this article, I am going to show you how you can improve your solution using a technique called memorization which uses a cache to store the intermediate results. This is a very useful technique to solve Dynamic Programming problems like the Knapsack problem. The good thing about Java is that it provides rich libraries which have classes like ConcurrentHashMap which can be used as a cache to store these intermediary results instead of re-computing them. This can drastically reduce the time to calculate the Nth Fibonacci number.
Labels:
coding
,
Coding Interview Question
,
Coding problems
,
Java 8
,
programming
Top 20 Stack and Queue Data Structure Interview Questions for 1 to 3 years Experienced
Both Stack and Queue are two of the crucial data structure which is very
useful in solving a lot of problems. They are not as fundamental data
structures as an
array or linked lists but they offer some unique advantages in terms of how you store and access
elements. Stack is commonly known as
LIFO data structure because the last element you put is the first one
to come out while Queue is known as FIFO data structure. The
elements are retrieved from the queue in the order they were inserted. Both
of these data structure has several usages and are used to solve many common
problems and to implement algorithms.
Labels:
Coding problems
,
data structure and algorithm
,
interview questions
,
queue
,
stack
Top 25 Recursion Interview Questions Answers for Coding Interviews
Recursion is often dreaded by programmers as its very difficult for many
programmers to visualize and understand, but it's a useful problem solving
technique which often result in better, simpler and more concise code than
their iterative solutions. That begin said, it's often difficult to come up
when to use
recursion
to solve a
coding problem. In general, you should use recursion if a problem can be broken down into
sub-problems which are exactly the same as original problem. For example,
finding a node in a binary tree. If you are asked to find a node with give value in a binary tree than you can solve this with recursion. Why? because you can breakdown the problem into subproblems which are exactly the same like finding a node in binary tree is same as finding the node in left subtree and right subtree.
Labels:
Coding problems
,
interview questions
,
recursion
Top 10 Matrix Coding Exercises for Programming interviews and Homework
Hello guys, if you are preparing for Coding interviews and solved 100+ data structure problems then you may know that Matrix is one of the under-rated topic for coding interviews but Matrix
coding problems are not that easy to solve and In this article, we'll
see some of the popular Matrix coding problems for coding interviews. I
actually learned a lot by solving Matrix related problems. I still remember
the first Matrix based problem I solve was about
how to multiply two matrices in Java
and I learned a lot about multi-dimensional array and nested loop in Java by
solving that problem. The second problem I solved was about transposing Matrix
and that was also quite challenging for me at that time but helped me to
further solidify my knowledge about loops and array in Java.
Labels:
Coding problems
,
interview questions
,
Matrix
Subscribe to:
Posts
(
Atom
)