Classic Coding InterView Questions
Tags: coding, interview, topcodingquestions
Published: 2023-06-13 15:36:54



1. Reverse a String:
   Write a function that takes a string as input and returns the reversed string.

   Example:
   Input: "hello"
   Output: "olleh"







2. Two Sum:
   Given an array of integers, find two numbers such that they add up to a specific target number. Return the indices of the two numbers.

   Example:
   Input: nums = [2, 7, 11, 15], target = 9
   Output: [0, 1] (indices of elements 2 and 7)



### Using Carry forward Technique

```csharp
public class Solution {
    public int[] TwoSum(int[] nums, int target) {
            int n = nums.Length;
            int sum = 0;
            int[] res = new int[2];

          
            for(int s =0;s<n;s++){
                sum =0;
                for(int e =s;e<n;e++){
                    if(s!=e){
                        sum= nums[s]+nums[e];
                        if(sum == target){
                            res = new int[]{s,e};
                        }
                    }
                }
            }

            return res;
    }
}

```
3. Palindrome Number:
   Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

   Example:
   Input: 121
   Output: true

4. FizzBuzz:
   Write a program that prints the numbers from 1 to n. But for multiples of three, print "Fizz" instead of the number, and for the multiples of five, print "Buzz". For numbers that are multiples of both three and five, print "FizzBuzz".

   Example:
   Input: n = 15
   Output: 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz

5. Merge Two Sorted Lists:
   Merge two sorted linked lists and return it as a new sorted list.

   Example:
   Input: l1 = 1->2->4, l2 = 1->3->4
   Output: 1->1->2->3->4->4

```csharp
public ListNode MergeTwoLists(ListNode l1, ListNode l2){
        if(l1 == null) return l2;
        if(l2 == null) return l1;
        if(l1.val <= l2.val){
            l1.next = MergeTwoLists(l1.next,l2);
            return l1;
        }else{
            l2.next = MergeTwoLists(l1,l2.next);
            return l2;
        }
    }

```
##If We have two sorted lists,below will be the answer.


```csharp
public void Merge(List<int> A, List<int> B){
       
     int n = A.Count;
     int m = B.Count;

	List<int> C = new List<int>(n+m);

    int p1 =0;
    int p2 = 0;
     int p3 = 0;

	while(p1<n && p2<m){
	   if(A[p1]<B[p2]){

		C[p3] = A[p1];
		p3++;
		p1++;
	   }
	   else{

              C[p3] = B[p2];
		p3++;
		p2++;
	   }

	}


	while(p1<n){

	    C[p3] = A[p1];
		p3++;
		p1++;
	}

	while(p2<m){

	    C[p3] = B[p2];
		p3++;
		p2++;
	}



    }
```


6. Find the Missing Number:
   Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the missing number.

   Example:
   Input: [3, 0, 1]
   Output: 2

```csharp
  public int MissingNumber(int[] nums) {
        int n = nums.Length;
        return n * (n + 1) / 2 - nums.Sum();
    }

```


7. Validate Parentheses:
   Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if:

   - Open brackets must be closed by the same type of brackets.
   - Open brackets must be closed in the correct order.

   Example:
   Input: "([{}])"
   Output: true


 

```csharp
public bool IsValid(string s) {
         // Get ready initial state (enforce element type)
        var k = new Stack<char>();
        // Evaluate each character for potential mismatch 
        foreach(char c in s) {
            // Push closing round bracket onto the stack
            if (c == '(') { k.Push(')'); continue; }
            // Push closing curly bracket onto the stack
            if (c == '{') { k.Push('}'); continue; }
            // Push closing square bracket onto the stack
            if (c == '[') { k.Push(']'); continue; }
            // for case:"]" nothing will add so k.count will be 0 and we already know for other imbalance condition
            if (k.Count == 0 || c != k.Pop()) return false;
        }

        //for case:"["
        return k.Count == 0;
    }
```


8. Reverse a Linked List:
   Reverse a singly linked list.

   Example:
   Input: 1->2->3->4->5
   Output: 5->4->3->2->1


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gziqy7k4ul05p8rt3yc1.jpeg)


```csharp
 public ListNode ReverseList(ListNode head) {


        ListNode h1 = head;
        ListNode h2 = null;
        ListNode t = null;

        while(h1!=null){
            t = h1;
            h1 = h1.next;
            t.next = h2;
            h2 = t;
        }
        
        return h2;
    }
```

9. Binary Search:
   Implement a binary search algorithm to find the position of a target value in a sorted array.

   Example:
   Input: nums = [2, 5, 7, 10, 15, 20], target = 7
   Output: 2


```csharp
public int Search(int[] nums, int target) {
        int left = 0;
        int right = nums.Length - 1;

        while(left <= right){
            int mid = (left + right) / 2;
            if(nums[mid] == target){
                return mid;
            }
            if(nums[mid] > target){
                right = mid - 1;
            }
            else{
                left = mid + 1;
            }
        }

        return -1;
    }

```

10. Find the Longest Substring Without Repeating Characters:
    Given a string, find the length of the longest substring without repeating characters.

    Example:
    Input: "abcabcbb"
    Output: 3 (for the substring "abc")