Data Structure: Interview Experience Part 9(Microsoft)
Data Structure: Interview Experience Part 9(Microsoft)
Design
1. Serialize and Deserialize BST
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize and deserialize a binary search tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary search tree can be serialized to a string and this string can be deserialized to the original tree structure.
The encoded string should be as compact as possible.
Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.
Practise: https://leetcode.com/problems/serialize-and-deserialize-bst/
2. Construct Binary Search Tree from Preorder Traversal
Return the root node of a binary
search
tree that matches the given
preorder
traversal.
(Recall that a binary search tree is a binary tree where for
every node, any descendant of node.left
has a value <
node.val
, and any descendant of node.right
has a value >
node.val
. Also recall that a preorder traversal displays the value of
the node
first, then traverses node.left
, then traverses node.right
.)
It’s guaranteed that for the given test cases there is always possible to find a binary search tree with the given requirements.
Example 1:
Input: [8,5,1,7,10,12]
Output: [8,5,10,1,7,null,12]
Constraints:
-
1 <= preorder.length <= 100
-
1 <= preorder[i] <= 10^8
-
The values of
preorder
are distinct.
Practise: https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/
3. Serialize and Deserialize Binary Tree
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.
Example:
You may serialize the following tree:
1
/ \
2 3
/ \
4 5
as "[1,2,3,null,null,4,5]"
Clarification: The above format is the same as how LeetCode serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.
Practise: https://leetcode.com/problems/serialize-and-deserialize-binary-tree/
4. Implement Trie (Prefix Tree)
Implement a trie with
insert
,
search
, and
startsWith
methods.
Example:
Trie trie = new Trie();
trie.insert("apple");
trie.search("apple"); // returns true
trie.search("app"); // returns false
trie.startsWith("app"); // returns true
trie.insert("app");
trie.search("app"); // returns true
Note:
-
You may assume that all inputs are consist of lowercase
letters
a-z
. - All inputs are guaranteed to be non-empty strings.
Practise: https://leetcode.com/problems/implement-trie-prefix-tree/
5. LRU Cache
Design and implement a data structure for
Least Recently Used (LRU) cache. It should support the following operations:
get
and
put
.
get(key)
- Get
the value (will always be positive) of the key if the key exists
in the cache, otherwise return -1.put(key, value)
- Set or insert the value if the key is not already present.
When the cache reached its capacity, it should invalidate the
least recently used item before inserting a new item.
The cache is initialized with a positive capacity.
Follow up:
Could you do both operations in
O(1)
time complexity?
Example:
LRUCache cache = new LRUCache( 2 /* capacity */ );
cache.put(1, 1);
cache.put(2, 2);
cache.get(1); // returns 1
cache.put(3, 3); // evicts key 2
cache.get(2); // returns -1 (not found)
cache.put(4, 4); // evicts key 1
cache.get(1); // returns -1 (not found)
cache.get(3); // returns 3
cache.get(4); // returns 4
Practise: https://leetcode.com/problems/lru-cache/
Hope this article is useful for people looking to learn and practice Data structure & Algorithm in swift, Please ❤️ to recommend this post to others 😊. Let me know your feedback. :)