1  # Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes. 
This problem require us to reorder a linked list by the node number, all odd nodes follow by all even nodes in the original linked list.
Although this seems complicated with a single pass, it can be done if we break down the linked list into two parts, an even part and an odd part.
With the two parts each contain nodes categorized with even or odd. Which allows us to simply concat these two linked list to form the final linked list, which will satisfy the requirement of this proble.
For details try to follow the implementation below.
1 

https://leetcode.com/problems/oddevenlinkedlist/description/
]]>1  # wordList = ["hot","dot","dog","lot","log","cog"] 
We are trying to turn a word, changing only 1 charater at a time to become the end word. And return the shortest path to that transformation. So we can visualize using a tree structure graph:
All words during the transformation, including the end word are children of the initial word. We can also conclude:
1 

1 

In everday life we evaluate numbers with operators in the form of a + b
this is called infix notatio, meaning the operator is in between the two values being operated on.
Polish notaiton, also known as prefix notation has operator in front of the operands in the form of + a b
.
You might’ve guessed it! Reversedpolishnotation, also called postfix notation has the operands before the operator a b +
, this problem is asking us to write a function that would take in an array of postfix operations and return the end value.
To solve this problem there are recursive strategies we can use, but it is most famous for the stack approach.
Start with a stack, append the two operands into the stack, use the operator to operate on the two operands(vals) and append the result back to the stack. (this way we always have the current val in the operation to work with).
In the end all the operands in the stack should be reduced to a single value via all the operators throughout the process.
1 

https://leetcode.com/problems/evaluatereversepolishnotation/
]]>1 

To solve this problem let’s define what is unique when we are iterating over a linked list with cycle in it.
If cycle does not exist we would eventually iterate to a null or None node, which doesn’t happen if the a cycle exist in the linked list.
But that is not enough to solve the problem, we must set a stopping point for the iteration otherwise we will end up in an inf loop.
To do that let’s make two pointers, one ahead of another by one node. If a cycle exist the slower one will eventually be caught up with the faster one.
1 

https://leetcode.com/problems/linkedlistcycle/description/
1 

Similarly we can use two pointers to detect a cycle in a linked list. Let’s first break down where they might meet.
A=head B=node cycle starts(target) C=somewhere in the cycle
If we use the same approach in the last problem, where both pointers start at A they will meet at C since the second pointer is twice as fast.
The slower pointer traverled x+y while faster pointer traverled x+y+z+y, and since we know faster pointer is twice as fast: x+y+z+y=2(x+y), meaning x=z.
At this pointer both ponter will be at C, meaning if we start another pointer from the head to traverl to B(x distance), the time it takes for the pointer at C to get to B will be the same(z distance) since we know x=z.
So, start another pointer from the head traverling at the same pase as the pointer at C, we should be able to find the cycle starting node B when they meet.
1 

https://leetcode.com/problems/linkedlistcycleii/description/
]]>1  # Given an array of nonnegative integers, you are initially positioned at the first index of the array. 
To clarify the question, we are given a list/array of vals. Each val represents the MAX number of jumps it can take, meaning if the val is 4 it can stop after moving 2 steps and switch onto another val for jump.
To approach this problem we must realize that we need to keep track of
1 

https://leetcode.com/problems/jumpgame/
1 

Similar to the last problem, in this one we want to not only find out whether we can reach the end with the jumps but also return the minimum number of jump we could take. Simply put, we just got more greedy!
To approach this we would have to keep track of:
1  def jump_game_ii(nums): 
https://leetcode.com/problems/jumpgameii/
1 

Simple and straightforward problem, as we iterate through the list we just need to keep track of:
1 

https://leetcode.com/problems/besttimetobuyandsellstock/
1 

Ok now we can sell stock multiple times throughout the course of the list, but only can hold one stock at a time.
We can approach this by think of our actions as we iterate through the stock prices, instead of finding the buyin and buyout pair that wold give us max profit. Each time we iterated on a price, we should either sell or buy a stock.
This way we can ensure to make max profit throughout our investment journey.
1 

1  # A robot is located at the topleft corner of a m x n grid (marked 'Start' in the diagram below). 
The problem state thats the robot may only move from down or to the right to get to the bottomright corner, this makes things easier.
For an example, I have a 2x2 matrix the only way I can get from topleft corner to bottomright corner is 2. Which is the total number of ways I can get to 0,1 and 1,0, the top and left of 2,2 (the destination). Hence we can derive the equation A[i][j] = A[j1][i] + A[j][i1], to represent the total number of ways to get to a location in the matrix.
We can solve this problme using depthfrist search, go through all possible path from the end to the start. But the draw back is that with dfs the time complexity will be O(N^4) which will time out on large matrix.
To solve that we can use dynamic programming instead, to approach the problem from start head to the end. The idea is to make an identical size matrix filled with 1s, and ignore the edges start traversal from 1,1 since there will be only 1 way to get through the edge spots. Similarly, filled out the possible ways of getting into each spot in that matrix by the formula above (left + top combination). In the end, the bottomright spot should contain the total number of ways to get to that spot from the beginning.
1 

https://leetcode.com/problems/uniquepaths/
1  # Follow up for "Unique Paths": 
The idea is similar to the previous problem, but added the ability to have obstacles.
We can use dynamic programming as well, filling out an all 1s array as we go. But we need to beware to escape the obstacles. To do that we initialize the matrix with all zeros, fill out the edge spots with 1s except for where obstacles are.
Then we may start the traversal similar to last problem, but escape all spots with 1s in the original matrix since they are obstacles.
1 

1 

This is a classic Google interview probem, and frequently used in userorientied application implementations.
In case the problem is not clear, we are designing a data structure that can hold a set amount of data, once the data limit is reached the oldest/firstinserted data will be erased from the data base since it’s considered ‘lease used’. With the same idea if we get or call a data no matter when it was inserted it will be now the newest or most recent used data.
In summary, this ds needs to be able to set a size and push out old data if size is reached. That sounds like a doubly linked list! This ds also needs to be able to retrive a data point and put it front of the list if it is called upon, sounds like we also need a dictionary or hash map to keep track of each node.
So our ideas are:
1  class Node(object): 
1  # Design a data structure that supports all following operations in average O(1) time. 
The first reactions are always to build a hash map ds, but the thing about hash map is that hashing values are unique to the data stored, mening there is no way to generate a hashing value correspond to a data without knowing what the data is.
Therefore, we must use a ds with index support to get a random data out of the data set, like linked list! But the removal will be an O(n) process.
Now it’s obvious we need hash map for the fast lookup time, and list for its indexing support. For this problem we must use both.
A list where all data is stored, and a hashmap to store the corresponding location(index) of that data in the list array.
To delete, we can simply find the index of the data using hashmap, and swap it with the last value in the list array, and pop it off. Constant actions = Constant time complexity
1  import random 
1  # Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. 
As many of you have seem before, this is a classic problem that utilizes stacks.
Parenthese pairs has two requirements:
1  def valid_parentheses(s_parens): 
Notice in the end we only return True when the stack is empty, this way if there is any unclosed paren in the stack it is still invalid.
https://leetcode.com/problems/validparentheses/
1  # Given n pairs of parentheses, write a function to generate all combinations of wellformed parentheses. 
Different then the problem above, this question is asking us to generate all possible valid combinations of parens given the number of pairs.
A common reaction is to use nested for loops, but it will be very difficultf since there are many variations not just ‘()()()’ or ‘((()))’.
To solve this problem we can use a technique call backtracking, it’s essentially a variation to recursion.
1  def generate_parenthesis(n): 
Notice the condition to stop the recursions are left < n and right < left, it restrict to be less then n, and also restricted the produciton of an right before a left
It’s helpful to draw out the recursion call stack, it’ll be much clear to you why the conditions are set up this way.
https://leetcode.com/problems/generateparentheses/description/
]]>1  # Given a 2D array binaryMatrix of 0s and 1s, implement a function getNumberOfIslands that returns the number of islands of 1s in binaryMatrix. 
The problem is asking us to identify the number of islands or cluster of 1s in a binary matrix. If we simply count the number of 1s it’ll be difficult to track which 1 belong to which cluster and thus will not give accurate answer. The depth first traversal approach is one that’s easy to understand, we’ll loop over the entire matrix and if the number is an 1, we will utlize dpeth first traversal to find all 1s linked or interlinked to the 1 that initiated the dfs.
First, we must create a function to loop over the matrix
1  def count_island(bm): # bm: binary matrix 
Following we can write a depth first traversal function to traverse all 1s in a cluster.
1  def dfs(bm, r, c): 