Interview Preparation

a repo for solutions for Leetcode and similar problems

Readme Card

LeetCode Problems:

Arrays & Strings

Name Solution Level Notes
Boats to Save People Link Medium Sort the list and check elements from the beginning and end
Container with Most Water Link Medium Finding max area by checking elements from two sides of array. max(temp,max_area). If height[left] < height [right] increase left
Find First and Last Position of Element in Sorted Array Link Medium Write two functions one for finding first pos, other one is for finding last pos. Use binary search
First Bad Version Link Easy Sorted array find value just like binary search
Longest Substring without Repeating Elements Link Medium Use two pointers and dict
Move Zeroes Link Easy Two for loops; one of them is for overwriting all elements by non-zero elements, the other one is for adding zeroes

Linked Lists

Name Solution Level Notes
Add Two Numbers Link Medium Estimate depth, calculate number itself, sum them up, return -1. Use carry approach simple, easy. My explanation on Leetcode
Linked List Cycle Link Easy Initialize two pointers, move one of them by 1, other one by 2. If there is a cycle, eventually those two pointers show the same value. Tortoise & Hare, Floyd’s algorithm My explanation on Leetcode
Merge Two Sorted Lists Link Easy Compare two elements at the same time, add rest of them
Reverse Linked List Link Easy Utilize iteration method to reverse linked list. Use three pointers prev,head and next and change head’s next position to prev and update each element’s place.

Maps

Name Solution Level Notes
4Sum II Link Medium The first two elements’ sum must equal to (-1) of last two elements’ sum
Contains Duplicate Link Easy Sets. My explanation on Leetcode
Group Anagrams Link Medium Sort strings and use them as keys
LRU Cache Link Hard Use Deque and Map to find solution
Majority Element Link Easy Count the occurrence of items solve the question
Two Sum Link Medium Use remaining number as a key. If you need to have 9, check remaining values 7,5,3 for 2,4,6 respectively. If you find the same remaining value in your list, you are done.

Math

Name Solution Level Notes
Add Binary Link Medium Check binary rules
Count Primes Link Easy Boolean array, iterate 2..sqrt(2) and set n*n
Missing Number Link Easy (n * (n + 1))/ 2
Robot Return to Origin Link Easy Calculate X & Y coordinate seperately
Single Number Link Easy Use sets, subtract 2*(set_version) from original one

inzva

Algorithm Training Beginner Set [Official solutions]

Problem & Solution
Easy Magic Trick
Ice Cream Yum Yum
Harun & Sami
Hopscotch
Meme Man
Merge Sort
Stringer Things
Stickers
Usain Bolt
The Pit

Data Structures

Name Example Notes
Doubly Linked List Link Next and previous
Singly Linked List Link Only next Insertion & Deletion O(1)

Algorithms

Name Example Notes
Binary Search Link Must be sorted, Divide the array by 2 and iterate
Sliding Window Link