AWS Network Firewall is a managed service that makes it easy to deploy essential network protections for all of your Amazon Virtual Private Clouds (VPCs). The service can be setup with just a few…
I was asked to White Board Binary Search Algorithm, Read on if you want to get insight in how to be really well at cracking them.
Even though I would never test anyone on White Board and this interview practice is slowly diminishing, I believe there are few traditional companies who are still going this route.
A binary search algorithm finds an item in a sorted list in O(lg(n)) time.
A brute force search would walk through the whole list, taking O(n)O(n) time in the worst case.
Let’s say we have a sorted list of numbers. To find a number with a binary search, we:
We can do this recursively, or iteratively. Here’s an iterative version:
CC#C++JavaJavaScriptObjective-CPHPPython 2.7Python 3.6 (beta)RubySwift
So the question is, “how many times must we divide our original list size (nn) in half until we get down to 1?”
n * \frac{1}{2} * \frac{1}{2} * \frac{1}{2} * \frac{1}{2} * … = 1n∗21∗21∗21∗21∗…=1
How many \frac{1}{2}21’s are there? We don’t know yet, but we can call that number xx:
n * (\frac{1}{2})^x = 1n∗(21)x=1
Now we solve for xx:
n * \frac{1^x}{2^x} = 1n∗2x1x=1n * \frac{1}{2^x} = 1n∗2x1=1\frac{n}{2^x} = 12xn=1n = 2^xn=2x
Now to get the xx out of the exponent. How do we do that? Logarithms.
The right hand side asks, “what power must we raise 22 to, to get 2^x2x?” Well, that’s just xx.
Careful: we can only use binary search if the input list is already sorted.
Pushing the publish button on your first video is both exciting and anxiety-inducing. Will anyone watch my video? Will people judge my videos? I have experienced this twice in the creation of two…
Years of learnings and nearly 12 months of dedicated work from a team passionate to improve the Seafood industry has brought us to this moment. The launch of Seafood Souq. If you’ve been following…
When the summer warmth hits you in a way that floods back a forgotten memory, it’s hard to pass on the nostalgia. As I cuddle with the puppy in bed, basking in the afternoon sun breaking through the…