8

In Silicon Valley show Episode 4 of Season 06, one coder is made fun of because he "brute forced a sorted list". You can see a screenshot of the code below.

For what I understand the code will return "index" if "element" is found on the sorted list. If not, it will just return -1. It's a basic comparison will all elements (which they find amusing)

Why is it funny to them? I don't get it.

enter image description here

2
  • Consider a list with 1000000 elements, then this takes 1000000 loops, in a binary search it only takes approximately lg2(1000000) ~= 20 (slightly costlier) loops.
    – Surt
    Commented Jan 16, 2021 at 20:16
  • @Surt Haha. I like this better. You should post it as an answer or example to asds answer below
    – tom johnes
    Commented Jan 16, 2021 at 20:43

3 Answers 3

9

Binary Search is used on sorted lists since it takes advantage of the property that the list is either decreasing or increasing.

Brute force can be done on any list, sorted or not.

O(n) vs O(log(n)) causes a huge performance difference as well.

Consider a list with 1000000 elements, then this takes 1000000 loops, in a binary search it only takes approximately lg2(1000000) ~= 20 (slightly costlier) loops.

1
  • 1
    Your link is broken Commented Feb 13, 2022 at 17:52
2

Brute force searching a sorted list is grossly inefficient, as the earlier answerers explained, but that's not the source of the humor in the scene for the characters, the actors or the viewers.

Without asking the actors, we can't really know if they even thought it was funny per se, though they probably understood that the humor in the scene comes from the characters' behavior rather than the technical aspect of Richard's mistake.

It's funny to Aristotle Athari's character, Gabe, because brute force searching a sorted list is the kind of amateurish mistake Gabe thinks himself incapable of making. But it could've been any other amateurish mistake.

It's funny to some viewers because Ethan is disrespecting Richard, and Richard is letting him. The other white men laugh at Richard, too. Dinesh feels empathy for Richard but is too cowardly to stand up for his friend. The amateurish mistake itself doesn't matter too much for the humor. You can laugh without knowing about binary search or any other algorithm that would be faster than brute force search.

However, the demonstration of Richard's mistake is confusing and unconvincing, distracting viewers such as yourself who have real knowledge of algorithms in practice but are not so quick to judge others for silly mistakes.

The writers could have explained Richard's folly better by using some little, inexpensive prop, like a brand-new deck of playing cards. Surely even after the budget needed for RussFest they could have afforded a little thing that you can get at some store for less than $10.

A better demonstration wouldn't have taken any more time than the confusing demonstration. For example, Ethan (George Basil) could've given Gabe the new deck of cards and asked him to pick out a card like the Ten of Diamonds or the Three of Clubs.

Then Gabe, pretending to be Richard, would look at the Ace of Spades, the Two of Spades, the Three of Spades, and so on and so forth through each of the spade cards, and then say something like "That's you, Richard." Of course if the deck is shuffled even just once, the requested card could be at any position in the deck.

Having Ethan, Gabe and the others laugh at Richard for a silly mistake he made years ago shows the characters are not just immature, but also lacking in technical knowledge.

Brute force searching a sorted list might be valid in an early step of test-driven development (TDD), when the test requires searching in a small sorted list. A later test might have a timeout for searching in a much larger sorted list, e.g., if it takes more than half a second to find the requested item in the larger sorted list, the test fails.

There's also the question of why was Richard even writing that index of function in the first place. How did he know the list was sorted? Was it because the list was an instance of SortedList? And wouldn't that class already provide an indexOf() function? In which case Richard's mistake of an inefficient algorithm would be compounded by his effort to reinvent that wheel.

However, it is entirely realistic that the characters in that scene would laugh at Richard. They might not make that kind of basic mistake, but they probably also wouldn't revolutionize compression like Richard has either. So Richard's the bigger man, at least until he tries to punch Ethan.

1

Consider a list with 1000000 elements, then this takes 1000000 loops, in a binary search it only takes approximately lg2(1000000) ~= 20 (slightly costlier) loops.

Absolute time might be around 3-4ns for the original loop and maybe 100-150ns (many cache misses and 50% branch prediction miss) for the binary loop, so 3000000-4000000ns to 2000-3000ns.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.