Top 8 Important Coding Interview Questions & Answers 2022 [For Freshers & Experienced]

Good foundations of data structures such as arrays, binary trees, hash tables and linked lists are essential. You must be aware of essential algorithms, methods and have a good hold over programming languages such as Java, especially if you are applying for programming jobs. You can definitely learn the answers to important coding interview questions, but practising the solutions extensively on your own is highly recommended.

Best 8 Coding Interview Questions and their Answers

Let us look at some of the most important coding questions that are asked in interviews hosted by massive companies such as Microsoft, IBM, Google, etc.

1. How can you find the first non-repeated character in a word?

In order to answer this question, you first need to understand what must be done to promote this function. A function needs to be written that accepts strings and returns the first non-repeated characters.

For example, in the word, ‘passage’, ‘p’ is the first non-repeated character or in the word, ‘turtle’, ‘u’ is the first non-repeated character. So, how do we solve this problem? We can create a table for storing the repetitions for all the characters and then select the first entries that are not repeated.

In order to write a code that will return the first non-repeated letters, we can use LinkedHashMap to store the character count. This HashMap follows the order of the insertion and characters are initialised in the same position as in the string. The scanned string must be iterated using LinkedHashMap to choose the required entry with the value of 1. 

Another way to approach this problem is by using firstNonRepeatingChar(String word). This allows the non-repeated character which appears first to be identified in a single pass. This approach used two storage to replace an interaction. This method stores non-repeated and repeated characters separately and when the iteration ends, the required character is the first element in the list. 

2. How can you remove duplicates from arrays?

First, you must use the LinkedHashSet (Set Interface) to retain the original insertion order of the elements into the set. You must use loops or recursion functions to solve these kinds of coding interview questions.

The main factor that we must keep in mind when dealing with arrays is not the elements that have duplicates. The main problem here is removing the duplicates instead. Arrays are static data structures that are of fixed length, thus not possible to be altered. So, to delete elements from arrays, you need to create new arrays and duplicate the content into these new arrays.

First, you must convert the arrays into Arraylists and then create LinkedHashSets from these ArrayLists. If input arrays contain a larger number of duplicates then it can result in multiple temporary arrays, thus increasing the cost of importing the content. This restriction enforces that we approach this problem in a manner that requires less memory and processing power.

We must remove the duplicates but not copy them into the resulting arrays, thus not deleting the duplicates entirely but simply replacing them with 0 as the default value.

3. How can we check if a number is a prime number?

This is one of the most common coding interview questions that involve finding out if the given number is a prime number or not. These kinds of programs are the foundations of algorithmic thinking as we must come up with solutions that are based on the fact that prime numbers are all natural numbers that cannot be divided by positive numbers other than 1.

We must write code to create loops that check each number starting from 1 to the target number to see if the target number is divisible by any other positive number other than itself or 1.

This function will lead us to the solution. When checking for a number that is especially large, then we can simply check till the square root of N, N being the target number. There is no need to check till N in the case of an eligible square root.

If the number is not divisible by 2, there is no need to check if it is divisible by other even numbers, thus decreasing the time required to find the solution. This is an optimized version of the solution where analysing the number before writing the solution comes in handy. 

4. How can you check if strings contain only digits?

If you wish to write regular expressions for checking if strings are only numbers or if they contain non-digit characters, you must first get comfortable with using character sets in Java regular expressions. Programming languages such as Java support regular expressions with the help of java.util.regex.Matcher class and java.util.regex.Pattern. Java.util.regex is a dedicated package for this purpose.

In order to validate the existence of only numbers using regular expressions, we can use code to analyse if strings contain a raw integer. We will check if the string contains only digits within 0 – 9. Even if the string contains digits but also other characters, it is not a simple numeric string. Regular expressions only check for integer numbers and do not consider dot characters (.), thus, making decimal numbers and floating points fail the test.

5. How can you reverse the words in a target sentence without the help of library methods?

This is also one of the very common coding interview questions. First, we must understand the requirement and how to fill the gap in this requirement. When faced with questions such as these, we must first concentrate on asking the right questions. Strings are nothing but sentences of decided characters that might contain a single word or multiple words.

A sentence might also be empty. For example, if we are given the sentence, ‘Programming is fun.’, we must reverse it to, ‘Fun is programming.’ effectively. We must use regular expressions in Java to divide the given strings into spaces followed by applying the reverse()method from the Collections utility class.

Once you are able to divide the strings using regex’\\s’, an array of words will be returned as a result. This also takes care of the words separated using multiple spaces. As soon as the array is returned, you can then choose to create ArrayLists from these arrays followed by using the Collections.reverse()method. This reverses ArrayLists and every word will be reinitialised in the reverse order.

Now, all that is left is using StringBuilder to concatenate multiple strings through ArrayList iteration. One must make sure that size is specified as StringBuilder resizing is a costly process in terms of processing power and memory. The resizing ends up creating new arrays from copying the content from the older arrays.

6. How can you replace or remove characters from strings? 

Suppose we have a string, ‘Woocommerce’, and we wish to replace the letter ‘r’ with ‘n’, there are multiple methods to achieve this. String classes in Java provide multiple approaches to replace characters inside strings using CharSequence and substrings.

You can easily call a replace method inside the string that will end up replacing the target character and returning the desired character as a result. Strings are immutable in programming languages such as Java.

Thus, every time these operations such as removal or replacements are performed on strings, new string objects are generated by default. There are 4 overloaded methods for replacing strings using Java:

  • replace(char oldChar, char newChar)
  • replaceAll(String regex, String replacement)
  • replace(CharSequence target, CharSequence replacement)
  • replaceFirst(String regex, String replacement)

CharSequence is one of the super interfaces for strings, StringBuilder and StringBuffer, allowing us to pass off any of the objects from these as arguments for this replacement method. replaceAll() ends up replacing every single match with replacement strings while replaceFirst() only replaces the first matches.

All in all, all of these are powerful methodologies that accept regular expression. Java.lang.String class allows all these overloaded methods that can easily replace single characters or substrings in Java.

 It is highly recommended to use replaceAll() as this replaces every occurrence of matching characters. Following this approach allows us to expect regular expression patterns, thus garnering more power. This method can also replace every comma with pipes for converting comma-separated files into pile delimited strings.

However, if one wishes to only replace a single character, one can just use the replace() method that takes the old and new given character into consideration.

Read: Java Interview Questions & Answers

7. How can you append texts to files in programming languages such as Java? 

Appending is very different as compared to the creation of new files and writing data into the new files. In cases of appending, files already exist and we need to simply add text at the end of the file. This is similar to log files as they are constantly updated with the system.

Log files are the perfect example of appending text as applications iteratively keep appending log details into these files. Logging frameworks are not required for this problem, but you must know how to append text into existing files. In order to solve this problem, you must be aware of convenience classes to write character files.

The class has constructors that assume the acceptability of default byte-buffer and character encoding. If you wish to specify the values yourself, you can simply construct the OutputStreamWriter using the FileOutputStream. The availability of files depends on the underlying platforms, which determine if the file might be created or not.

A few platforms allow files to be initialised for writing functions using a single FileWrite or multiple file-writing objects. However, constructors from this class will be failing once the involved file is already initialised. FileWriter is used for writing character streams and FileOutputStream can write raw byte streams.

Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.

8. How can you find the largest or smallest number in an array of integers?

For this solution, we must code a function or method that can find the largest or smallest number from arrays that are full-fledged integers. We must first create a source file in Java using the name and copy the written code here for compilation and execution.

We can use two variables that we can refer to as ‘largest’ and ‘smallest’ to store the maximum and minimum values respectively from the arrays. The smallest number can be initialised using integer.MIN_VALUE and the largest can be initialised using integer.MAX_VALUE.

With every iteration of the loops that you have initiated, you can compare current numbers with ‘largest’ and ‘smallest’ and update them accordingly. Arrays do not override the toString method in Java, so you can use Arrays.toString() for printing the contents of the target arrays.

You can use this static method to directly call the main function. You must then pass the random arrays through this method to check if the maximum and minimum values have been returned accurately. You can also choose to automate this testing through Unit tests in your IDE.

Checkout: Top 4 Computer Skills to Put in Your Resume


Courses that are focused on programming are not just great for solving problems but also additionally increase your chances of being selected when applying for jobs. If you wish to learn more about Java programming and more advanced coding problems, you can enrol yourself into a comprehensive course such as upGrad’s Master of Science in Computer Science or the Full Stack Development Course.

Become a Full Stack Developer

0 replies on “Top 8 Important Coding Interview Questions & Answers 2022 [For Freshers & Experienced]”

Let’s do it!
No, thanks.