face-smile-horns33 Must Know CP Question

A small request:

If you find value in what we’re doing, please subscribe to our YouTube channel and share this initiative with others in your network. Together, we can build a stronger tech community. ❤️

1. Suppose you have an array with 11 digits. Five numbers each appear exactly twice, and one number appears only once. How can you find the single number without using any extra array or map, and with a time complexity of O(n)?

function singleNumber(nums) {
  let result = 0;
  for (let i = 0; i < nums.length; i++) {
    result ^= nums[i]; //bitwise XOR operation
  } 
  return result;
}

389. Find the Differencearrow-up-right: You are given two strings s and t. String t is generated by random shuffling string s and then add one more letter at a random position. Return the letter that was added to t.

Brute force Approach

class Solution {
public:
    char findTheDifference(string s, string t) {
        if (s.size() < 1)
            return t[0];

        unordered_map<char,int> mapping;

        for (int i = 0; i < t.size(); i++) {
            mapping[t[i]]++;
        }
        for (int i = 0; i < s.size(); i++) {
            mapping[s[i]]--;
        }

        for (auto it : mapping) {
            if (it.second != 0) {
                return it.first;
            }
        }

        return 'a';
    }
};

Optimal Approach

2. Reverse the array without any additional array! (Two Pointer Approach)

3. Given a number. Find the sum of the digits of the number ?

4. Merge these two sorted arrays using one loop?

5. Find out which is the last /previous palindrome number of a given number. (Example: 20 is given, so 11 is the palindrome number) [Reverse a Number]

6. An array of letters from a-z is given. Find out the first non-repeated letter. (like array [A, B,V,C,A,C,V,P] is given, here B is the first non-repeated letter).

Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.

Example 1:

Example 2:

8. How To Check If a Value Is A Power Of 2 Or Not ?

9.1. Find All Substrings of a String

9.2. Find Substring in a String

Time Complexity: O(N * M)

Best Complexity: KMP Algorithm = O(N + M)

10. How do you swap two numbers without using a third variable

11. GCD of Two Numbers

12. A string of 9 characters will be given. There will be 3 blocks of 3 characters in the String. each character will have a numerical value. you need to find the sum of the numerical values of each block and then finally concave the answer of each block and form a string. rw-x-r-rw Here r = 4 , x = 2, w = 1, - = 0 What is the answer?

13. Write code to Calculate frequency of characters in a string

14. Write code Check if the given string is Palindrome or not

15. Find the Missing Number Between 1-N: 268. Missing Numberarrow-up-right

16. Write code to Check if two strings are Anagram or not : 242. Valid Anagramarrow-up-right

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

17. 2nd Smallest Number in an Array

18. Write code of Perfect number (Divisors of a Number)

20. Find non-repeating characters in a string

Combining these two parts:

  • The time complexity for inserting all elements into the priority queue (building the max-heap) is O(nlog⁡n)

  • The time complexity for extracting the k-th largest element is O(klog⁡n).

Overall Time Complexity

Therefore, the overall time complexity of the code is: O(nlog⁡n+klog⁡n)

In the worst case, k is proportional to n making the complexity: k=== n

O(nlogn+nlogn) = O(nlogn)

23. Find a number is a prime or not

  • Complexity: Root(N)

  • Complexity : N*Log(Log(N)

24. Circular rotation of an array by K positions

25.Dynamic Programming: Climbing Stairsarrow-up-right

27. Diameter of Binary Treearrow-up-right: The diameter of a binary tree is the length of the longest path between any two nodes in a tree.

28. Linked List Cycle:arrow-up-right Determine if the linked list has a cycle in it.

  1. Use two pointers, walker and runner.

  2. walker moves step by step. runner moves two steps at time.

  3. if the Linked List has a cycle walker and runner will meet at some point.

29. Middle of the Linked List:arrow-up-right If there are two middle nodes, return the second middle node.

Each time, walker go 1 step while runner go 2 steps. When runner arrives at the end, walker will arrive right in the middle.

Given an integer array nums, find the subarray with the largest sum, and return its sum.

Example 1:

Last updated