Postingan

Menampilkan postingan dengan label data structure and algorithm

Top 5 Free Data Structure and Algorithm Courses for Java and C Programmers

Gambar
Data Structure and Algorithm is one of the essential topics for programmers, both to get a job and do well on Job. A good knowledge of data structure and algorithm is the foundation of writing good code. If you are familiar with essential data structures e.g. array, string, linked list, tree, map and advanced data structure e.g. tries, AVL trees etc and know when to use which data structure and compute the CPU and memory cost of your code in terms  Even though you don't need to write your own array, linked list or hashtable, given every major programming SKD provides them e.g. JDK or C++ STL library, you will need to understand them so that you can use them in right place. Using a right data structure can drastically improve the performance of an algorithm. Ideally, we should all learn the data structure and algorithm in our school and college but it mostly it didn't happen. Most of the programmers, including myself, only get introduced to a data structure in our computer scien...

How to Find Middle Element of Linked List in Java in Single Pass

Gambar
ow do you find the middle element of LinkedList in one pass is a programming question often asked Java and non-Java programmers in telephonic Interview. This question is similar to checking palindrome or calculating the factorial, where Interviewer sometimes also ask to write code. In order to answer this question candidate must be familiar with LinkedList data structure i.e. In the case of singly LinkedList, each node of Linked List contains data and pointer, which is the address of next Linked List and the last element of Singly Linked List points towards the null. Since in order to find middle element of Linked List you need to find the length of linked list, which is counting elements till end i.e. until you find the last element of Linked List. What makes this data structure Interview question interesting is that you need to find the middle element of LinkedList in one pass and you don’t know the length of LinkedList. This is where candidates logical ability puts into the test...

Difference between Stable and Unstable Sorting Algorithm - MergeSort vs QuickSort

Gambar
Recently in one on the interview, after some initial questions about sorting algorithms e.g. how do you write QuickSort or difference between QuickSort and MergeSort, the interviewer asked about do you understand the difference between stable and unstable sorting algorithm? This question was new to my reader, so he says, Sorry, never heard about that. The story ended there, and Interviewer moved on to next question but like many of us, my reader went on to find more about unanswered questions and ultimately he asks me what is the meaning of a stable and unstable sorting algorithm? Some of you might be heard about it and many of you might not know about this distinction, I'll try to answer this question in this article. A sorting algorithm is said to be stable if it maintains the relative order of numbers/records in the case of tie i.e. if you need to sort 1 1 2 3 then if you don't change order of those first two ones than your algorithm is stable, but if you swap them then it...

How to find the largest and smallest number in given Array in Java

Gambar
Hello guys, today I am going to discuss one of the frequently asked programming interview questions to find the largest and smallest number from an integer array . This question is commonly asked on telephonic interviews and the first round for screening candidates. This coding problem is in the same league of other frequently asked algorithmic question likeFibonacci, Palindrome, Prime, and Power of two checks. They are easy but you will find them difficult during the interview because of the pressure, particularly if you have not solved them before. Sometimes even if you have solved the problem one time, you tend to do mistakes because you haven't understood them properly. So, always take your time to understand the question. Many interviewers further evaluate a candidate's problem-solving skill by asking him to solve the problem by different ways like if he solves the problem using a loop then asking him to do it without the loop. If you know theRecursion you can do it, but i...

How to generate an Array of Prime numbers in Java - Sieve of Eratosthenes Algorithm Example

Gambar
Hello guys, I have said many times that a good knowledge of Data Structure and Algorithms is the first step towards becoming a better programmer and that's why I share a lot of Data structure and Algorithm stuff in this blog. To continue the tradition, I am going to share an interesting algorithm today, The Sieve of Eratosthenes algorithm, which can be used to generate prime number up to a given number. There are many occasions when you need to generate all prime numbers up to a specified integer and one algorithm which is most often used to generate prime numbers is the Sieve of Eratosthenes Algorithm. Surprisingly, not many developers know about this algorithm, particularly Java programmers, which is mainly because not doing enough competitive programming. Sieve of Eratosthenes is an ancient Greek algorithm to find all prime numbers up to a given number and named after the famous Greek mathematician Eratosthenes. He was the first man to calculate the circumference of the earth ...

Grokking Algorithms Book Review - Best Data Structure and Algorithm Book for Beginner Programmer and Computer Science Students

Gambar
Hello guys, I have read many books on data structures and algorithms likeIntroduction to Algorithms by Thomas H. Corman and Algorithm design manual by Steve S. Skiena, so when I come to know about this book, I thought, just another book on algorithms, but I was wrong. This is not just another book on algorithms but one of the most interesting books you will ever read on Algorithms and Data structure . It doesn't cover all the data structure and algorithms you see in Computer Science but whatever it covers, it does really well and that's what matters most for beginner programmer or Computer Science students. Let's accept it, data structure and algorithms is interesting but at the same time they are very complex and hard to understand, especially, if not explained very well and that's where this book rocks. It explains things in very easy language and most importantly with a context, which helps to understand the topic better. For example, when Aditya talked about array ...

How to declare and initialize a List (ArrayList and LinkedList) with values in Java - Arrays.asList() Example

Gambar
Hello guys, today, I am going to share with you a useful tip to initialize a List like ArrayList or LinkedList with values, which will help you a lot while writing unit tests and prototypes. Initializing a list while declaring is very convenient for quick use, but unfortunately, Java doesn't provide any programming constructs like collection literals of Scala, but there is a trick which you can use to declare and initialize a List at the same time. This trick is also known as initializing List with values . I have used this trick a lot while declaring list for unit testing and writing demo programs to understand an API etc and today I'll you'll also learn that. If you have been using Java programming language for quite some time then you must be familiar with the syntax of an array in Java and how to initialize an array in the same line while declaring it as shown below: String[] oldValues = new String[] {"list" , "set" , "map"} or even shorter...

Binary Tree PreOrder Traversal in Java - Recursion and Iteration Example

Gambar
Unlike a linked list and array which are linear data structure and can only be traversed linearly, there are several ways to traverse a binary tree because of its hierarchical nature. The tree traversal algorithms are mainly divided into two types, the depth-first algorithms , and breadth-first algorithms . As their name suggests, in a depth-first algorithm, the tree is traversed downwards (towards the depth) before the next sibling is visited, the PreOrder , InOrder and PostOrder traversal of a binary tree is actually depth-first traversal algorithms. On the breadth-first algorithm, also known as level order traversal, the entire breadth of the tree is traversed before moving to the next level, hence it is also known as level order traversal. There are other algorithms to traverse a binary tree as well e.g. Monte Carlo tree search, which concentrates on analyzing the most promising moves, but the pre-order, post-order, and in-order traversal are the most popular ways to traverse a bin...

Bucket Sort in Java with Example - How Algorithm Works

Gambar
In recent years, one of the questions I have increasingly seen in programming job interviews is about constant time sorting algorithms like do you know any O(n) sorting algorithm?  how do they work? When I first encountered this question, I had no idea whether we can sort in constant time because even some of the fastest sorting algorithms like QuickSort or MergeSort takes O(N log N) time for sorting on their average case. After some research, mainly by going through the classic CLRS book and thisDS and Algorithms course by Tim Buchalka and Goran Lochert on Udemy, I come to know that there indeed are some constant time or linear time sorting algorithms like bucket sort, counting sort, and radix sort, which can sort an array in O(n) time but they work with only a special set of input. The idea of bucket sort is quite simple, you distribute the elements of an array into a number of buckets and then sort those individual buckets by a different sorting algorithm or by recur...

How to Find Multiple Missing Integers in Given Array of Numbers with Duplicates in Java?

Gambar
It's been a long time since I have discussed anycoding oralgorithm interview questions, so I thought to revisit one of the most popular array based coding problem 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 candidate and further test their ability to adapt to frequent changes. In the past I have demonstrated how to find the missing number in a sorted array as well on the unsorted integer array in Java using BitSet (see here), but, with just one missing number and without any duplicates, which kinda make those problems a bit easier. That makes the problem somewhat easy but what do you do if interviewer tells you that array contains duplicates  and more than one numbers are mis...