Complete-Preparation

πŸŽ‰ One-stop destination for all your technical interview Preparation πŸŽ‰

View the Project on GitHub

Leetcode Problem 200-299

203. Remove Linked List Elements 🌟

O(N) Time and O(1) Space

O(N) Time and O(1) Space, recursive


206. Reverse Linked List 🌟

O(N) Time and O(1) space, iterative

O(N) Time and O(1) space, recursive

  1. Divide the list in two parts - first node and rest of the linked list.
  2. Call reverse for the rest of the linked list.
  3. Link rest to first.
  4. Fix head pointer

210. Course Schedule II 🌟🌟

Topological Sort + DFS

READ


216. Combination Sum III 🌟🌟

Backtracking Solution


217. Contains Duplicate 🌟

O(N^2) Time and constant space

O(N log N) Time and constant space

O(N) Time and O(N) Space


219. Contains Duplicate II 🌟

Sliding window + hashmap

Sliding window + set


221. Maximal Square 🌟🌟

Dynamic Programming

READ


222. Count Complete Tree Nodes 🌟🌟

O(N) Time

O(log N * log N) Time


226. Invert Binary Tree 🌟

O(N) Time recursive solution

O(N) Time O(N) stack iterative solution


227. Basic Calculator II 🌟🌟

Using Stack

Without Stack

The approach works similar to Approach 1 with the following differences :


228. Summary Ranges 🌟

Intuitive Solution

Intuitive Solution


229. Majority Element II 🌟🌟

Brute force

O(N) Time and O(1) space

Moore’s Voting Algorithm


230. Kth Smallest Element in a BST 🌟🌟

Inorder traversal

Get kth while traversing.

Above method in iterative way.


231. Power of Two 🌟


232. Implement Queue using Stacks 🌟

O(1) AMORTIZED Time solution


234. Palindrome Linked List 🌟

Brute force

Optimized


235. Lowest Common Ancestor of a Binary Search Tree 🌟

O(N) Time recursive solution

O(N) Time iterative solution


236. Lowest Common Ancestor of a Binary Tree 🌟🌟

O(N) Time and O(N) Space

O(N) Time recursive solution


237. Delete Node in a Linked List 🌟

Dumb Question πŸ˜‚πŸ€£πŸ€£

MUST READ:

let’s analyze why this problem **isn’t a good interview question.**

The whole point of asking any candidates a linked list problem is to test if the candidates think about edge cases, including:

1. Dereferencing Null Pointer, usually targeting tail pointer
2. When given Head is None
3. When there are duplications in the list

This question specifically mentioned all the above edge cases and extracted them out for you Someone who can solve this problem might not even think of all the edge cases, which can backfire on them in real interview settings


238. Product of Array Except Self 🌟🌟

Intuitive Solution

Using Extra space

Space optimization

Read


242. Valid Anagram 🌟

O(N logN) Time and constant space

O(N) Time and O(N)=O(26) constant space

Only 2 loops


258. Add Digits 🌟

Brute Force

Mathematical solution


260. Single Number III 🌟🌟

O(N^2) Time

O(NlogN) Time Sorting solution

O(N) Time HashMap solution

O(N) Time O(1) Bit Manipulation solution

Idea is to use property of XOR:

1. xor(a, a) = 0
2. xor(a, 0) = xor(0, a) = a
3. xor(1, 1) = xor(0, 0) = 0
4. xor(1, 0) = xor(0, 1) = 1
5. xor(a, b) = c => xor(a, c) = b and xor(b, c) = a

Let two distinct elements be: first and sec. Now, firstXorSec := xor(first, sec) = xor(nums[i], 0), 0 <= i < [n:= nums.size()], why? because nums has odd and even freq. elements, doing xor will cancel out elements with even freq. using the 1. property of xor. So, at the end, xor of only odd freq. elements i.e first and sec remains.

Now, first != sec => there is at least 1 set bit in firstXorSec. Using 4. property of xor, one of first and sec, must have a set bit (Let it be first). We find/ denote this rightmost set bit using mask: has all bits 0 except the rightmost set bit in firstXorSec. So, we can divide the elements in nums in two groups A and B, where:

1. groupA: (num & mask) == 1 i.e bit is set
2. groupB: (num & mask) == 0 i.e bit is not set

Clearly, both first and sec belong to different groups and since, all other elements occurs in pair, so, we can safely again use 1. property of xor to generate first. Finally, using 5. property of xor, we have sec = xor(firstXorSec, first)


268. Missing Number 🌟

O(NlogN) by Sorting

O(N) Time


278. First Bad Version 🌟

O(log N) Time solution


283. Move Zeroes 🌟

O(N) Time solution

O(N) Time snowball solution


287. Find the Duplicate Number 🌟🌟

O(N^2) Brute force

O(NlogN) Sorting

O(N) Time O(N) space, Hash Table

O(N) Time O(1) space, Floyd’s Cycle Detection Algorithm


290. Word Pattern 🌟

Hashmap Solution


299. Bulls and Cows 🌟🌟

Counting Solution