Interview Questions | Learn for Master - Part 4
• ### Leetcode Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see thek numbers in the window. Each time the sliding window moves right by one position.

For example,
Given nums = [1,3,-1,-3,5,3,6,7], and k = 3.

Therefore, return the max sliding window as [3,3,5,5,6,7].

Note:
You may assume k is always valid, ie: 1 ≤ k ≤ input array’s size for non-empty array.

[Read More...]
• ### LeetCode Recover Binary Search Tree (java)

Two elements of a binary search tree (BST) are swapped by mistake.

Recover the tree without changing its structure.

Note:
A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?

Java Solution

We can use a stack to do an in order traverse of the tree, as it can produce a sorted list of a binary search tree. In the process, we keep track of two pointers pre, cur:

If we encounter pre.val > cur.val the first time, we get the first error node p1 = pre,

[Read More...]
• ### Leetcode Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.

For example:
Given the below binary tree and sum = 22,

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

Java Solution

[Read More...]
• ### Leetcode Permutation Sequence

The set [1,2,3,…,n] contains a total of n! unique permutations.

By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):

1. “123”
2. “132”
3. “213”
4. “231”
5. “312”
6. “321”

Given n and k, return the kth permutation sequence.

Note: Given n will be between 1 and 9 inclusive.

Java solution:

Refernce:

https://leetcode.com/problems/permutation-sequence/

http://fisherlei.blogspot.com/2013/04/leetcode-permutation-sequence-solution.html

[Read More...]
• ### Leetcode Flatten Nested List Iterator (Java)

Given a nested list of integers, implement an iterator to flatten it.

Each element is either an integer, or a list — whose elements may also be integers or other lists.

Example 1:
Given the list [[1,1],2,[1,1]],

By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].

Example 2:
Given the list [1,[4,]],

By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].

[Read More...]
• ### LeetCode Shuffle an Array (Java)

Shuffle a set of numbers without duplicates.

Example:

Java Solution

[Read More...]
• ### LeetCode Linked List Cycle (java)

Given a linked list, determine if it has a cycle in it.

Follow up:
Can you solve it without using extra space?

Java Solution

[Read More...]
• ### LeetCode Ransom Note (Java)

Given  an  arbitrary  ransom  note  string  and  another  string  containing  letters from  all  the  magazines,  write  a  function  that  will  return  true  if  the  ransom   note  can  be  constructed  from  the  magazines ;  otherwise,  it  will  return  false.

Each  letter  in  the  magazine  string  can  only  be  used  once  in  your  ransom  note.

Note:
You may assume that both strings contain only lowercase letters.

Java Solution

[Read More...]
• ### Leetcode Jump Game I & II (Java)

Jump Game I

Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Determine if you are able to reach the last index.

For example:
A = [2,3,1,1,4], return true.

A = [3,2,1,0,4], return false.

Analysis:
We maintain the max step can be reached so far.  If an index i is larger than the current max step that can be reached, it returns false.

Java Solution:

Jump Game II

Given an array of non-negative integers,

[Read More...]
• ### Leetcode mini parser

Given a nested list of integers represented as a string, implement a parser to deserialize it.

Each element is either an integer, or a list — whose elements may also be integers or other lists.

Note: You may assume that the string is well-formed:

• String is non-empty.
• String does not contain white spaces.
• String contains only digits 0-9, [, – ,, ].

Example 1:

Example 2:

Analysis

Define num to store the current number,

[Read More...]
Page 4 of 11« First...23456...10...Last »