Leonid Kogan - Cantabile, Paganini
Kogan - La Campanella
Jascha Heifetz - Caprice No. 24
Leonid Kogan - Cantabile, Paganini
Kogan - La Campanella
Jascha Heifetz - Caprice No. 24
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
The left subtree of a node contains only nodes with keys less than the node’s key.
The right subtree of a node contains only nodes with keys greater than the node’s key.
Both the left and right subtrees must also be binary search trees.
Inorder traversal, then compare each value…
|
|
Or use static variable to store the previous val:
|
|
At each node we must satisty
|
|
Given n, how many structurally unique BST’s (binary search trees) that store values 1…n?
For example,
Given n = 3, there are a total of 5 unique BST’s.
|
|
|
|
Given n, generate all structurally unique BST’s (binary search trees) that store values 1…n.
For example,
Given n = 3, your program should return all 5 unique BST’s shown below.
|
|
Similar to I, we pick (1…n) as root, recursivly get all left and right subtrees.
We pick one from left subtree and one from right subtree, attach them to current root and return all the possibilities.
|
|
Given a binary tree, return the preorder traversal of its nodes’ values.
For example:
Given binary tree {1,#,2,3},
|
|
|
|
|
|
Given a binary tree, return the inorder traversal of its nodes’ values.
For example:
Given binary tree {1,#,2,3},
|
|
|
|
Given a binary tree, return the postorder traversal of its nodes’ values.
For example:
Given binary tree {1,#,2,3},
|
|
|
|
|
|
Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between).
For example:
|
|
|
|
|
|
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.
Calling next() will return the next smallest number in the BST.
Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree.
|
|
|
|
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [−2,1,−3,4,−1,2,1,−5,4],
the contiguous subarray [4,−1,2,1] has the largest sum = 6.
|
|
|
|
Find the contiguous subarray within an array (containing at least one number) which has the largest product.
For example, given the array [2,3,-2,4],
the contiguous subarray [2,3] has the largest product = 6.
|
|
|
|
Converts input String to Long
|
|
Insersion and Deletion
|
|
Print all unique combinations of factors of a positive integer. For example given 24:
|
|
Reverse DFS all possible division combinations. Keep track of current dividend, try all divisors. Use backtracking to gather all all possible division combinations.
|
|
|
|
Given an integer n, return the number of trailing zeroes in n!.
|
|
Value types are created on the stack, and reference types are created on the heap.
Both are stored in computer RAM.
Each thread gets a stack, while there’s typically only one heap for the application.
When a function is called, a block is reserved on the top of the stack for local variables and some bookkeeping data in a LIFO order. Freeing a block from the stack is nothing more than adjusting one pointer.
Unlike the stack, there’s no enforced pattern to the allocation and deallocation of blocks from the heap; you can allocate a block at any time and free it at any time. This makes it much more complex to keep track of which parts of the heap are allocated or free at any given time; there are many custom heap allocators available to tune heap performance for different usage patterns.
The stack is attached to a thread, so when the thread exits the stack is reclaimed.
The heap is typically allocated at application startup by the runtime, and is reclaimed when the application (technically process) exits.
The size of the stack is set when a thread is created.
The size of the heap is set on application startup, but can grow as space is needed (the allocator requests more memory from the operating system).
The stack is faster because the access pattern makes it trivial to allocate and deallocate memory from it, while the heap has much more complex bookkeeping involved in an allocation or free.
Each byte in the stack tends to be reused very frequently which means it tends to be mapped to the processor’s cache, making it very fast.
Another performance hit for the heap is that the heap, being mostly a global resource, typically has to be multi-threading safe, i.e. each allocation and deallocation needs to be (typically) synchronized.
static
to signify it is nested; without static
, it would be inner class
|
|
|
|
|
|
|
|
overview
operations
balance and self-adjusting
overview
rotation
overview
types
overview
types
Operations
Compare
Similarities
When to use which
Vector
|
|
Overview
HashMap
TreeMap
HashTable
LinkedHashMap
Set
List
Map
BST
HashTable
Overview
BFS
DFS
|
|
- DFS: stack
+ recursive
|
|
+ iterative
|
|
Contents:
Access modifiers
| modifier | access in same package | access in different package |
| ———— | ——————————— | —————————————- |
| private | no | no |
| public | yes | yes |
| default | yes | no |
| protected| yes | only if extend class |
final
keyword
synchronized
keyword
volatile
keyword
static
variable
static
cannot be used for classstatic
is related to class not objectstatic
method
static class
throw
keyword
object vs. class
static
and instance
method
abstract
class and interface
public
, private
,protected
, default
, or constantspublic final
, only has constantsinstanceof
and isInstance(Object obj)
instacneof
is a keyword but isInstance()
is a methodinstanceof
check whether the object is type of particular class or subclassisInstance()
only used to identify if object is of a particular classpass by value and pass by reference
==
and equals()
==
is used to compare the references of the objectsequals()
can compare the values of two objectsStringBuffer
and StringBuild
StringBuffer
is synchronized but StringBuild
is notfinal
, finally
,finalize()
final
: a final variable acts like constant, a final method cannot be overridden, a final class is immutablefinally
: handles exception, clean up after try blockfinalize()
: method helps in garbage collection, invoked before an object is discarded by the garbage collector, allowing it to clean up its statestatic block and init block
object reflections
Overload, Override
-Overloading and overriding are complementary things, overloading means the same method name but different parameters, and overriding means the same method name in a subclass with the same parameters. So its not possible for overloading and overriding to happen at the same time because overloading implies different parameters.
|
|
polymorphism
inheritance
extends
keywordmultiple inheritance
|
|
abstraction
encapsulation
ArrayList and vector
Sort objects in lists
HashMap and HashTable
List interface
Set interface
Arrays and ArrayList
ArrayList and LinkedList
Advantage of iterator
Preferred declaration
List<String> list = new ArrayList<String>()
not ArrayList<String> list = new ArrayList<String>()
Iterator access and index access
有一个输入字符,然后我有一个英文字典,说白了就是字符串数组,然后写一个function返回字典里拥有输入字符串里所有字母(非数字或者空格等符号,就是纯字母)的最短的那个字符串。举个例子:
输入字符串”SR 456 T”,字符串数组里有”SORT”,而且是最短的,那么就返回它
|
|