Coding Interview Questions

Tech jobs are growing fast, but competition is getting tougher. Good interview preparation is now more important than ever to land your dream coding job.

Nowadays coding interviews needs many skills beyond just writing code. Companies ask about programming languages, frameworks, databases, system design, and problem-solving.

We have created the most complete collection of coding interview questions and answers online. Our guides cover everything you need, including Programming Languages, Web Frameworks, Databases, Devloper roles and more.

Each question includes clear explanations, different solutions, and real examples you can use in interviews. Our content stays updated with the latest trends.

Don’t let interview stress stop you from getting your next job. Explore our question guides today and boost your confidence for coding interviews.

Coding Interview Questions

SQL Interview Questions
PHP Interview Questions
JAVA Interview Questions
React Interview Questions
NodeJS Interview Questions
ExpressJS Interview Questions
Python Interview Questions
Pandas and NumPy Interview Questions
C# Interview Questions
OOPs Interview Questions
Python OOPs Interview Questions
Java OOPs Interview Questions
C# OOPs Interview Questions
ASP.NET Interview Questions
Selenium Interview Questions
API Testing Interview Questions
Web API Interview Questions
Rest API Interview Questions
Software engineer Interview Questions
Front-End Developer Interview Questions
Back-End Developer Interview Questions
Full Stack Developer Interview Questions
Android Developer Interview Questions
iOS Developer Interview Questions
Web Developer Interview Questions

Java Coding Interview Questions and Answers

Que 1. How do you reverse a string in Java?

Answer:
You can reverse a string using the StringBuilder class:

String original = "Hello";
String reversed = new StringBuilder(original).reverse().toString();
System.out.println(reversed); // Output: olleH

Alternatively, for manual reversal, convert the string to a character array and swap characters from both ends.

Que 2. How do you check if a string is a palindrome?

Answer:
A string is a palindrome if it reads the same backward as forward.

public boolean isPalindrome(String str) {
    int i = 0, j = str.length() - 1;
    while (i < j) {
        if (str.charAt(i++) != str.charAt(j--)) return false;
    }
    return true;
}

Que 3. How do you find the factorial of a number using recursion?

Answer:

public int factorial(int n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}

This approach uses the recursive definition of factorial: n! = n * (n-1)!.

Que 4. How do you check if a number is prime?

Answer:

public boolean isPrime(int n) {
    if (n <= 1) return false;
    for (int i = 2; i <= Math.sqrt(n); i++) {
        if (n % i == 0) return false;
    }
    return true;
}

Check divisibility up to the square root of n for efficiency.

Que 5. How do you remove duplicates from an array in Java?

Answer:

public int[] removeDuplicates(int[] arr) {
    return Arrays.stream(arr).distinct().toArray();
}

Alternatively, use a Set to store only unique values.

Que 6. How do you find the missing number in an array of 1 to n?

Answer:
Use the formula for the sum of first n natural numbers.

public int findMissing(int[] arr) {
    int n = arr.length + 1;
    int total = n * (n + 1) / 2;
    int sum = Arrays.stream(arr).sum();
    return total - sum;
}

Que 7. How do you swap two numbers without using a third variable?

Answer:

a = a + b;
b = a - b;
a = a - b;

This method uses arithmetic operations to swap values in-place.

Que 8. How do you sort an array in descending order?

Answer:

Integer[] arr = {5, 1, 4, 2};
Arrays.sort(arr, Collections.reverseOrder());

Use Integer instead of int for compatibility with Collections.reverseOrder().

Que 9. How do you count the occurrence of each character in a string?

Answer:

Map<Character, Integer> map = new HashMap<>();
for (char ch : str.toCharArray()) {
    map.put(ch, map.getOrDefault(ch, 0) + 1);
}

This map will hold each character as a key and its count as the value.

Que 10. How do you check if two strings are anagrams?

Answer:

public boolean isAnagram(String s1, String s2) {
    char[] a = s1.toCharArray();
    char[] b = s2.toCharArray();
    Arrays.sort(a);
    Arrays.sort(b);
    return Arrays.equals(a, b);
}

Sorting and comparing both strings ensures they have the same characters.

Que 11. How do you find the second largest number in an array?

Answer:

int first = Integer.MIN_VALUE, second = Integer.MIN_VALUE;
for (int num : arr) {
    if (num > first) {
        second = first;
        first = num;
    } else if (num > second && num != first) {
        second = num;
    }
}

This approach tracks the two highest unique values during iteration.

Que 12. How do you implement a basic Singleton pattern in Java?

Answer:

public class Singleton {
    private static Singleton instance;
    private Singleton() {}
    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}

This ensures that only one instance of the class exists.

Que 13. How do you find the frequency of words in a string?

Answer:

Map<String, Integer> map = new HashMap<>();
String[] words = str.split("\\s+");
for (String word : words) {
    map.put(word, map.getOrDefault(word, 0) + 1);
}

Splitting by space and using a map tracks word counts efficiently.

Que 14. How do you reverse a linked list?

Answer:

public ListNode reverse(ListNode head) {
    ListNode prev = null;
    while (head != null) {
        ListNode next = head.next;
        head.next = prev;
        prev = head;
        head = next;
    }
    return prev;
}

This iterative approach changes pointers to reverse the list.

Que 15. How do you implement bubble sort in Java?

Answer:

public void bubbleSort(int[] arr) {
    int n = arr.length;
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

This is a simple sorting algorithm that repeatedly swaps adjacent elements if they are in the wrong order.

Python Coding Interview Questions and Answers

Que 16. How do you reverse a string in Python?

Answer:
You can reverse a string using slicing:

s = "hello"
reversed_s = s[::-1]
print(reversed_s)  # Output: "olleh"

Alternatively, you can use the reversed() function with join().

Que 17. How do you check if a number is prime in Python?

Answer:

def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, int(n ** 0.5) + 1):
        if n % i == 0:
            return False
    return True

This checks divisibility up to the square root of the number for optimal performance.

Que 18. How do you swap two variables in Python?

Answer:
Python allows direct variable swapping without a third variable:

a, b = 5, 10
a, b = b, a

This is one of Python’s syntactic advantages over many other languages.

Que 19. How do you find the factorial of a number using recursion?

Answer:

def factorial(n):
    if n <= 1:
        return 1
    return n * factorial(n - 1)

This is a direct implementation of the recursive factorial formula.

Que 20. How do you remove duplicates from a list in Python?

Answer:
You can use set() to remove duplicates:

unique_items = list(set([1, 2, 2, 3, 4, 4]))

Note: This does not preserve the original order. Use dict.fromkeys() for ordered results.

Que 21. How do you count the frequency of elements in a list?

Answer:

from collections import Counter
nums = [1, 2, 2, 3]
count = Counter(nums)

Counter creates a dictionary-like object with elements as keys and counts as values.

Que 22. How do you check if a string is a palindrome?

Answer:

def is_palindrome(s):
    return s == s[::-1]

It compares the string with its reverse using slicing.

Que 23. How do you merge two dictionaries in Python?

Answer:
In Python 3.9+:

dict1 = {'a': 1}
dict2 = {'b': 2}
merged = dict1 | dict2

In older versions, use {**dict1, **dict2}.

Que 24. How do you find the second largest number in a list?

Answer:

def second_largest(nums):
    unique = list(set(nums))
    unique.sort()
    return unique[-2]

Removing duplicates ensures accuracy in the result.

Que 25. How do you find common elements between two lists?

Answer:

common = list(set(list1) & set(list2))

Using set intersection finds common items efficiently.

Que 26. How do you reverse a list in Python?

Answer:
Use slicing or the reverse() method:

lst = [1, 2, 3]
lst.reverse()  # In-place
# Or
reversed_lst = lst[::-1]  # New reversed list

Que 27. How do you sort a list of dictionaries by a specific key?

Answer:

data = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 20}]
sorted_data = sorted(data, key=lambda x: x['age'])

This sorts the list based on the ‘age’ key in ascending order.

Que 28. How do you remove all vowels from a string?

Answer:

def remove_vowels(s):
    return ''.join([c for c in s if c.lower() not in 'aeiou'])

List comprehension is an efficient way to filter characters.

Que 29. How do you generate Fibonacci numbers up to n?

Answer:

def fibonacci(n):
    a, b = 0, 1
    while a < n:
        print(a, end=' ')
        a, b = b, a + b

This uses a while loop for an efficient, memory-safe solution.

Que 30. How do you flatten a nested list?

Answer:

from itertools import chain
nested = [[1, 2], [3, 4]]
flat = list(chain.from_iterable(nested))

This flattens a two-level nested list into a single list.

SQL Coding Interview Questions and Answers

Que 31. How do you retrieve the second highest salary from an Employee table?

Answer:
You can use the LIMIT and ORDER BY clause (MySQL/PostgreSQL) or TOP with subqueries (SQL Server):

SELECT DISTINCT salary
FROM Employee
ORDER BY salary DESC
LIMIT 1 OFFSET 1;

In SQL Server:

SELECT MAX(salary) 
FROM Employee 
WHERE salary < (SELECT MAX(salary) FROM Employee);

This fetches the second highest unique salary.

Que 32. How do you find duplicate records in a table?

Answer:

SELECT name, COUNT(*) 
FROM Employees 
GROUP BY name 
HAVING COUNT(*) > 1;

This identifies duplicate entries based on the name column. You can replace name with any column of interest.

Que 33. How do you delete duplicate rows but keep one in SQL?

Answer:

DELETE FROM Employees
WHERE id NOT IN (
  SELECT MIN(id)
  FROM Employees
  GROUP BY name, department
);

This query deletes duplicates based on name and department, keeping the row with the smallest id.

Que 34. How do you retrieve the top 3 highest-paid employees in each department?

Answer:

SELECT * FROM (
  SELECT *, 
         DENSE_RANK() OVER (PARTITION BY department ORDER BY salary DESC) as rank
  FROM Employees
) t
WHERE rank <= 3;

This uses window functions to rank employees within departments.

Que 35. How do you calculate the running total of sales?

Answer:

SELECT order_id, amount,
       SUM(amount) OVER (ORDER BY order_id) AS running_total
FROM Orders;

This uses a window function to compute cumulative totals.

Que 36. How do you find customers who placed more than one order on the same day?

Answer:

SELECT customer_id, order_date, COUNT(*) as order_count
FROM Orders
GROUP BY customer_id, order_date
HAVING COUNT(*) > 1;

This helps identify high-activity customers or possible duplicate entries.

Que 37. How do you transpose rows to columns in SQL (Pivot)?

Answer (MySQL/PostgreSQL):

SELECT 
  customer_id,
  MAX(CASE WHEN month = 'Jan' THEN sales ELSE 0 END) AS Jan,
  MAX(CASE WHEN month = 'Feb' THEN sales ELSE 0 END) AS Feb
FROM sales_data
GROUP BY customer_id;

Pivoting is useful for generating summarized reports.

Que 38. How do you retrieve employees who have not been assigned to any department?

Answer:

SELECT e.name 
FROM Employees e
LEFT JOIN Departments d ON e.dept_id = d.id
WHERE d.id IS NULL;

A LEFT JOIN with IS NULL identifies unmatched records.

Que 39. How do you select rows with the maximum value in a group?

Answer:

SELECT * 
FROM Orders o
WHERE amount = (
  SELECT MAX(amount)
  FROM Orders o2
  WHERE o2.customer_id = o.customer_id
);

This finds each customer’s highest-value order.

Que 40. How do you find consecutive login dates for users?

Answer:

SELECT user_id, login_date,
       LAG(login_date) OVER (PARTITION BY user_id ORDER BY login_date) AS previous_login
FROM logins;

Use LAG() or LEAD() to compare current and previous rows for identifying consecutive activities.

#C Coding Interview Questions and Answers

Que 41. How do you reverse a string in C#?

Answer:
You can reverse a string using the ToCharArray() method along with Array.Reverse():

string input = "hello";
char[] chars = input.ToCharArray();
Array.Reverse(chars);
string reversed = new string(chars);
Console.WriteLine(reversed); // Output: "olleh"

This is a simple and efficient way to reverse a string in C#.

Que 42. How do you check if a number is a palindrome in C#?

Answer:

int num = 121;
int temp = num, reversed = 0;
while (temp > 0)
{
    int digit = temp % 10;
    reversed = reversed * 10 + digit;
    temp /= 10;
}
bool isPalindrome = num == reversed;

This logic reverses the number and compares it with the original.

Que 43. How do you swap two numbers without using a third variable?

Answer:

int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;

This technique uses arithmetic operations to swap values in-place.

Que 44. How do you find the factorial of a number using recursion?

Answer:

int Factorial(int n)
{
    if (n <= 1)
        return 1;
    return n * Factorial(n - 1);
}

This recursive approach returns n! for any integer n.

Que 45. How do you check if a string is an anagram of another in C#?

Answer:

bool IsAnagram(string s1, string s2)
{
    var arr1 = s1.ToCharArray();
    var arr2 = s2.ToCharArray();
    Array.Sort(arr1);
    Array.Sort(arr2);
    return new string(arr1) == new string(arr2);
}

Sorting and comparing the strings ensures both have the same characters in the same frequency.

Que 46. How do you reverse a linked list in C#?

Answer:

public class Node
{
    public int data;
    public Node next;
}

Node Reverse(Node head)
{
    Node prev = null, current = head;
    while (current != null)
    {
        Node next = current.next;
        current.next = prev;
        prev = current;
        current = next;
    }
    return prev;
}

This iteratively reverses the pointers in the list.

Que 47. How do you remove duplicates from a list in C#?

Answer:

List<int> numbers = new List<int>{1, 2, 2, 3, 3, 4};
List<int> unique = numbers.Distinct().ToList();

Using Distinct() removes duplicates in a single line.

Que 48. How do you find the maximum occurring character in a string?

Answer:

string input = "programming";
var result = input.GroupBy(c => c)
                  .OrderByDescending(g => g.Count())
                  .First().Key;

This uses LINQ to group characters and find the one with the highest frequency.

Que 49. How do you implement a Fibonacci series using iteration?

Answer:

int a = 0, b = 1, n = 10;
Console.Write(a + " " + b + " ");
for (int i = 2; i < n; i++)
{
    int c = a + b;
    Console.Write(c + " ");
    a = b;
    b = c;
}

This generates the first n Fibonacci numbers using a loop.

Que 50. How do you find all prime numbers up to a given number?

Answer:

bool IsPrime(int n)
{
    if (n <= 1) return false;
    for (int i = 2; i <= Math.Sqrt(n); i++)
        if (n % i == 0)
            return false;
    return true;
}

for (int i = 2; i <= 100; i++)
    if (IsPrime(i))
        Console.Write(i + " ");

This uses a standard approach with square root optimization for performance.