Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
senthil kumar
Why O(m log n) for binary search instead of 0(log n)

senthil kumar

Apr 3, 2024

In case of integer, we just need to check the value of the node with the search value, it will be 0(1) operations.

Where else in string, most of the language will compare each character to character in the default "==" or equal methods . Hence on each step we will compare the whole string ( by one by one character)

[ Explanation from Chat GPT]

Consider a binary search tree (BST) with the following string keys: "apple", "banana", "cherry", "date", "elderberry". The BST might look something like this:

    banana    /      \ apple    elderberry    \      /       \  cherry date

Now, let's say we want to search for the key "date". Here's how the search would proceed:

  1. Start at the root ("banana"). "date" is greater than "banana", so go right.
  2. Now we're at "elderberry". "date" is less than "elderberry", so go left.
  3. Now we're at "date". We've found the key we're looking for.

Each comparison involves comparing the search key ("date") with the key of a node. Since these keys are strings, each comparison takes O(m) time, where m is the maximum length of the two strings. In this case, m is 10 ("elderberry" has 10 characters).

Since a well-balanced BST has a height of log n, where n is the number of keys, we need to do log n comparisons in the worst case. Therefore, the total time complexity of the search operation is O(m log n).

Note that this is a worst-case scenario. If the BST is not well-balanced, the height could be as much as n, in which case the time complexity would be O(m). Also, if the keys are short strings, m will be small, so the time complexity could be close to O(log n).

Comparing two strings using the equals operation (== in most languages, .equals() in Java) is not a constant time operation, O(1). It's actually a linear time operation, O(m), where m is the length of the shortest string.

Here's why: when you compare two strings for equality, you need to check each pair of corresponding characters in the two strings. If the strings are of length m and n, you need to check min(m, n) pairs of characters. In the worst case, the strings are equal or only differ at the last character, so you need to check all min(m, n) pairs. Therefore, the time complexity is O(m), where m is the length of the shortest string.

For example, consider the strings "hello" and "hells". To determine that they are not equal, you need to compare all five pairs of characters:

  1. 'h' == 'h' -> true
  2. 'e' == 'e' -> true
  3. 'l' == 'l' -> true
  4. 'l' == 'l' -> true
  5. 'o' != 's' -> false

So, string comparison is not a constant time operation. It depends on the length of the strings.

Comparing two integers also takes time proportional to the number of digits, but it's usually faster than comparing strings. Here's why:

When you compare two integers, the computer doesn't compare them digit by digit like it does with strings. Instead, it compares them as whole numbers. This is a single operation that takes constant time, O(1), regardless of the size of the integers.

However, this is a bit of an oversimplification. In reality, the time it takes to compare two integers depends on the number of bits used to represent them. Most modern computers use 32 or 64 bits to represent an integer. So, you could say that comparing two integers takes O(1) time with respect to the number of bits.

But if you're dealing with very large integers that can't be represented with 32 or 64 bits, then the time complexity to compare them would be O(m), where m is the number of digits. This is because large integers are usually represented as arrays of smaller integers or strings, and comparing them involves comparing these arrays or strings, which takes linear time.

So, in general, comparing two integers is faster than comparing two strings of the same length. But if the integers are very large, the time complexity could be the same.

0

0

Comments
Comments

On this page