Microsoft Interview Experience — Part 3(Solution in Swift)

Previous: Microsoft Interview Experience — Part 2 (Solution in Swift)

Microsoft Interview Experience — Part 3(Solution in Swift)

1. Implement LRU Cache with given public api’s

public func set(value: Any, for key:Key)
public func get(_ key: Key) -> Any?

Solution:

2. Given a non-empty binary tree, find the maximum path sum.
For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.

Input: [-10,9,20,null,null,15,7]

-10
/ \
9 20
/ \
15 7

Output: 42

Solution:

3. Given a sorted array of distinct integers, Find the Magic index or Fixed point in the array such that a[i] == i

let a = [-1, 0, 1, 2, 4, 10]

Ans: 4

Solution: