Postingan

Menampilkan postingan dengan label Coding Interview Question

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...

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 ...

How to write a C like Sizeof function in Java?

Gambar
If you have just started learning Java and came from C background then you might have noticed some difference between Java and C programming language, e.g., String is an object in Java and not a NULL-terminated character array. Similarly, there is is no sizeof() operator in Java. All primitive values have predefined size, e.g., int is 4 bytes, char is 2 byte, short is 2 byte, long and float is 8 byte and so on. But if you are missing sizeOf operator then why not let's make it a coding task? If you are Ok, then your next job is to write a method in Java, which can behave like sizeOf() operator/function of C and returns the size in bytes for each primitive numeric types, i.e. all primitive types except boolean. Many of you think, why we are not including boolean? Isn't that it just need 1 bit to represent true and false value? Well, I am nog, including boolean in this exercise because the size of boolean is not strictly defined in Java specification and varies between different ...

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...

Top 20 String Algorithm Questions from Coding Interviews

Gambar
In this article, we are going to see top 20 String based coding interview question and their solution to help programmers better prepare for interviews. The string is one of the most important data structure and available in almost every programming language like Java, C, C++, Python, Perl, and Ruby. Though there implement differ the essence remains same like String is NULL terminated character array in C butString is an object in Java, again backed by character array. The string is also available on weekly typed languages like Python and Perl.  This is why you will always find some String based coding question on programming interview. Even in your college days, you would have solved lots of coding problems based upon String like reversing String in place, checking if String is a palindrome, checking if two strings are an anagram of each other, calculating permutations of String etc. The coding questions from programming interviews are also not very different from that, but yes it...

2 Ways to Check if a String is Rotation of Other in Java - Algorithms

Gambar
Write a program to check if one String is a rotation of another String is a common coding problem you will find on programming job interviews.  A String is said to be a rotation of another String, if it has the same length, contains the same characters, and they were rotated around one of the characters. For example,  String"bcda" is a rotation of "abcd" but "bdca" is not a rotation of String "abcd". One of the simplest solutions to this interesting problem is first to check if two String has the same length, if not then one String cannot be the rotation of another. If they are of the same length then just create another String by concatenating first String with itself, now check if second String is a substring of this concatenated String or not, if yes, the second String is a rotation of first. You might be wondering, how does this solution work? Well, you can rotate the String around some character and if you join the String with itself then it...

How to find the 3rd (kth) Node from end or tail in a linked list in Java

Gambar
Hello guys, the problem to find the 3rd element from the end in a singly linked list or Kth node from the tail is one of the tricky but frequently asked linked list problems in Programming job interviews. I know you can easily solve this problem by moving from tail to head or in the reverse direction but the main challenge here is to solve the problem in just one pass. That means, you can not traverse the linked list again and you cannot traverse backward because it's a singly linked list. So what do you think? Isn't this problem challenging? Well, I did struggle when I saw this problem very first time but once you understand the logic and some tricks to solve a linked list based problem like Recursion then it would be easy for you, and that's what you will learn in this article. If you have practiced some linked list problems likefinding the length, inserting elements, or deleting elements then you should be familiar with how to traverse through a linked list. Here, we...

How to implement Binary Search in Java without Recursion - Iterative algorithm

Gambar
This week’s task is to implement a binary search in Java, you need to write both iterative and recursive binary search algorithm. In computer science, a binary search or half-interval search is a divide and conquer algorithm which locates the position of an item in a sorted array. Binary search works by comparing an input value to the middle element of the array. The comparison determines whether the element equals the input, less than the input or greater. When the element being compared to equals the input the search stops and typically returns the position of the element. If the element is not equal to the input then a comparison is made to determine whether the input is less than or greater than the element. Depending on which it is the algorithm then starts over but only searching the top or a bottom subset of the array's elements. If the input is not located within the array the algorithm will usually output a unique value indicating this. Binary search algorithms typically ...