DEV Community

Tpoint Tech
Tpoint Tech

Posted on

How to Prepare for Coding Interviews (With Practice Questions)

Introduction

Preparing for a coding interview is important if you are looking for a job in technology, as programming plays a major role in advancing technology for the next generation and also contributes a lot to research and innovation. If you have a strong grip in programming, you can crack the coding interview with a handsome package, because coding is in high demand in the IT sector.

The IT sector provides a high number of jobs with a handsome salary and also provides work-life balance. Preparing for a coding interview requires time and effort. You should start preparing for coding interviews from the early days of your college.

How to prepare for a Coding Interview

To prepare for a coding interview, you must have proper planning. To prepare for a coding interview, you need to cover the DSA topic. To have a strong grip on DSA, you can practice programming on an online platform. There are some very popular websites like LeetCode, CodeChef, Tpoint Tech, HackerRank, etc. These websites help you a lot in preparing for a coding interview, as this website will cover all the topics and types of questions which are asked in a Coding Interview. You also must have good projects in your resume, like website and desktop applications. These projects must have some unique features and also should be very useful in real life.

Important Questions for Coding Interview

Question on Array

  1. Largest element in array: In this program, you need to find the largest element in an array

// Java program to find the largest element
import java.util.*;
class Main {
public static void main(String[] args) {
int[] arr = {23, 15 , 4 , 78, 67};
int max = 0;
for(int i = 0; i < arr.length; i++) {
max = Math.max(max, arr[i]);
}
System.out.println(max);
}
}

** Output**
78

  1. Longest Common Prefix: In this question, you need to find the longest common prefix string amongst an array of strings.

// Java program to find the longest common string
import java.util.*;
class Main {
public static String longestCommonPrefix(String[] str) {
String prefix = str[0];
for(int index = 1; index < str.length; index++) {
while(str[index].indexOf(prefix) != 0) {
prefix = prefix.substring(0, prefix.length()-1);
}
}
return prefix;
}
public static void main(String[] args) {
String[] arr = {"flowers", "flow", "flight"};
System.out.println(longestCommonPrefix(arr));
}
}

Output:
fl

  1. 3Sum: In this question, you need to find the sum of three elements in an array such that their indices should not be the same and adding those numbers should give 0.

// Java program to write the code for 3Sum
import java.util.*;
class Reference {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
// Sort the array
Arrays.sort(nums);
for (int i = 0; i < nums.length - 2; i++) {
// Skip duplicate elements for i
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
int j = i + 1;
int k = nums.length - 1;
while (j < k) {
int sum = nums[i] + nums[j] + nums[k];
if (sum == 0) {
// Found the triplet with zero sum
ans.add(Arrays.asList(nums[i], nums[j], nums[k]));
// Skip duplicate elements for j
while (j < k && nums[j] == nums[j + 1]) {
j++;
}
// Skip duplicate elements for k
while (j < k && nums[k] == nums[k - 1]) {
k--;
}
// Move the pointers
j++;
k--;
} else if (sum < 0) {
// Sum is less than zero, increment j to increase the sum
j++;
} else {
// Sum is greater than zero, decrement k to decrease the sum
k--;
}
}
}
return ans;
}
}
// Driver Code to execute the program
class Main {
public static void main(String[] args) {
Reference solution = new Reference();
int[] nums = {1, 4, 3, -7, 8, 9};
List<List<Integer>> result = solution.threeSum(nums);
for (List<Integer> idx : result) {
System.out.println(idx);
}
}
}

Output
[-7, 3, 4]

  1. Rotate Array: In this type of program, you have an array and an integer value, suppose n. You are asked to rotate the array up to n times towards the left. The element in the last position will come to the front position. The elements in the array will be arranged in different positions after the rotation of the array up to n times because every element in the array will either shift or move to the right.

In this way, the elements will remain the same but will be arranged at different positions. You are asked to optimise the program in the interview. So you need to explain your program with a different approach that will give the same result but utilise less time and space complexity.

Given below is a medium-level program to rotate the array elements to the right by k times, where k is a non-negative integer.

// Java program to rotate an array to the right
import java.util.*;
class Main {
public static void rotate(int[] nums, int k) {
k %= nums.length;
int n = nums.length;
reverseNum(nums, 0, n-1);
reverseNum(nums, 0, k-1);
reverseNum(nums, k, n-1);
}
public static void reverseNum(int[] nums, int start, int end) {
while(start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
public static void main(String[] args) {
int[] nums = {1,2,3,4,5,6,7}; // Expected output: 5 6 7 1 2 3 4
int k = 3;
rotate(nums, k);
for(int element: nums) {
System.out.print(element + " ");
}
}
}

Output
5 6 7 1 2 3 4

Questions on String

String is an important topic to prepare for a coding interview. Most of the time, questions are asked on topics related to string. Given below are the basic and medium-level questions which are important for the interview.

  1. Longest Common Prefix: In this question, you are given an array of strings and you are asked to write a program that gives the longest common prefix as an output present among the array elements.

// Java program to print the longest common prefix
import java.util.*;
class Main {
public static String longestCommonPrefix(String[] strs) {
String prefix = strs[0];
for(int index = 1; index < strs.length; index++) {
while(strs[index].indexOf(prefix) != 0) {
prefix = prefix.substring(0, prefix.length() - 1);
}
}
return prefix;
}
public static void main(String[] args) {
String[] strs = {"tpoint","tpoint tech","tpoint website"};
System.out.println(longestCommonPrefix(strs));
}
}

Output
tpoint

  1. Valid Parentheses: In this question, you are given a string containing the brackets, and you are asked to check whether a given string is valid or not. A string is valid if it satisfies the following three conditions.

• First, every open bracket must be closed by its corresponding same type of bracket.
• The bracket must be placed in the correct order.
• Every close bracket has a corresponding open bracket of the same type.

// Java program to write a code for valid parentheses
import java.util.*;
class Main {
public static boolean isValid(String s) {
Stack<Character> st = new Stack<>();
for(int i =0; i<s.length();i++) {
char ch = s.charAt(i);
if(ch =='(' || ch =='{' || ch =='[') {
st.push(ch);
} else if(ch ==')') {
if(st.size()==0 || st.peek()!='(') {
return false;
} else {
st.pop();
}
} else if(ch ==']') {
if(st.size()==0 || st.peek()!='[') {
return false;
} else {
st.pop();
}
} else if(ch =='}') {
if(st.size()==0 || st.peek()!='{') {
return false;
} else {
st.pop();
}
} else {
// nothing
}
}
return st.isEmpty();
}
public static void main(String[] args) {
String s = "() [] {}"; // expected output is true
System.out.println(isValid(s));
}
}

Output
true

Questions on Linked List

The Linked List topic is very important from an interview point of view. A linked list is a linear collection of data elements which consist of nodes, which contain a data element and the memory address of the next node to which the current node is pointing. Linked List provides better efficiency for performing operations like sorting, inserting, and deleting data in a program because of its dynamic structure. Given below are some important questions for an interview on a Linked List.

  1. Remove Duplicates from Sorted List: This is a basic level question which contains duplicate nodes, and you are asked to remove the duplicate elements such that each element appears only once. You are also asked to return the linked list sorted as well.

// Java program to remove duplicates from sorted list
public class Main {
static class ListNode {
int val;
ListNode next;
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public static ListNode deleteDuplicates(ListNode head) {
ListNode node = head;
while (node != null && node.next != null) {
if (node.val == node.next.val) {
node.next = node.next.next;
} else {
node = node.next;
}
}
return head;
}
public static void printList(ListNode node) {
while (node != null) {
System.out.print(node.val + " ");
node = node.next;
}
System.out.println();
}
public static void main(String[] args) {
ListNode head = new ListNode(1, new ListNode(1, new ListNode(2)));
head = deleteDuplicates(head);
printList(head);
}
}

Output
1 2

  1. Rotate List: In this type of question, you are given a linked list and an integer, suppose k. You are asked to rotate the list towards the right by k times. • Original List : [1, 2, 3, 4, 5]; k = 2 • Updated List: [4, 5, 1, 2, 3]

This is a medium-level question asked in the coding interview. Given below is a demonstration of the Rotate List in Java programming

// Java program to write the code for rotating list
public class Main {
// Definition for singly-linked list
static class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
// Rotate Right function
public static ListNode rotateRight(ListNode head, int k) {
if (head == null || k == 0) return head;
// Find length and tail
ListNode tail = head;
int len = 1;
while (tail.next != null) {
tail = tail.next;
len++;
}
// Handle cases where rotation is unnecessary
if (len == 1 || k % len == 0) return head;
// Normalize k
k = k % len;
// Find new tail (len - k - 1 steps)
ListNode newLast = head;
for (int i = 0; i < len - k - 1; i++) {
newLast = newLast.next;
}
// set the new head
ListNode newHead = newLast.next;
newLast.next = null;
tail.next = head;
return newHead;
}
//Function to print the Linked List
public static void printLinkedList(ListNode head) {
ListNode current = head;
while (current != null) {
System.out.print(current.val);
if (current.next != null) System.out.print(" -> ");
current = current.next;
}
System.out.println();
}
// Driver Code
public static void main(String[] args) {
// Manually creating linked list: 1 -> 2 -> 3 -> 4 -> 5
ListNode head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(3);
head.next.next.next = new ListNode(4);
head.next.next.next.next = new ListNode(5);
int k = 2;
System.out.print("Original List: ");
printLinkedList(head);
head = rotateRight(head, k);
System.out.print("Updated List After rotation by " + k + ": ");
printLinkedList(head);
}
}

Output
Original List: 1 -> 2 -> 3 -> 4 -> 5
Update List After rotation by 2: 4 -> 5 -> 1 -> 2 -> 3

Conclusion

This article described the preparation for a coding interview. To be well prepared for an interview, you should have a proper understanding of the syntax of the language and also the looping statements and data types use in programming.
If you are looking for more of this type of article, I suggest you visit the Tpoint Tech Website, where you can find various articles on computer programming, along with an online compiler as well, where you can run your code and see the result easily.

Top comments (0)