|
| 1 | +--- |
| 2 | +title: "Java vs Python via the lens of an interview problem" |
| 3 | +date: 2025-07-14 |
| 4 | +--- |
| 5 | + |
| 6 | +[[toc]] |
| 7 | + |
| 8 | +## Intro |
| 9 | + |
| 10 | +I recently had an interesting (to me atleast) thought about how certain assumptions about language features would make some class of bugs non-existent and being a concern for a programmer. In this post, I will give an instance of such a difference between Java and Python via the lens of a binary search algorithm - a technique to solve a class of interview coding problems. |
| 11 | + |
| 12 | +Python has become my programming language of choice in recent years for implementing interview coding problems. A binary search is a technique to avoid doing extra work in certain special scenarios when the data to be searched is ordered. It brings down worst case time of search from an order of `n` to `log n`, where `n` is the size of the data set. |
| 13 | + |
| 14 | +## Binary Search |
| 15 | + |
| 16 | +### Java version |
| 17 | + |
| 18 | +```java |
| 19 | +/** |
| 20 | + * Performs a binary search on a sorted integer array to find the index of a target value. |
| 21 | + * |
| 22 | + * @param arr - array of values ordered in ascending. |
| 23 | + * @param target - target value to find in the array. |
| 24 | + * |
| 25 | + * @return index of the target value if found, otherwise -1. |
| 26 | +*/ |
| 27 | +public int binarySearch(int[] arr, int target) { |
| 28 | + |
| 29 | + if (arr == null) { |
| 30 | + return -1; |
| 31 | + } |
| 32 | + |
| 33 | + int low = 0; |
| 34 | + int high = arr.length - 1; |
| 35 | + |
| 36 | + while (low <= high) { |
| 37 | + // Calculate the middle index using a safer way to prevent overflow |
| 38 | + int mid = low + (high - low) / 2; |
| 39 | + |
| 40 | + if (arr[mid] == target) { |
| 41 | + return mid; |
| 42 | + } else if (arr[mid] < target) { |
| 43 | + low = mid + 1; |
| 44 | + } else { |
| 45 | + high = mid - 1; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return -1; |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +### Python version |
| 54 | + |
| 55 | +```python |
| 56 | +from typing import List |
| 57 | + |
| 58 | +def binary_search(arr: List[int], target: int) -> int: |
| 59 | + """ |
| 60 | + Performs a binary search on a sorted integer array to find the index of a target value. |
| 61 | +
|
| 62 | + Args: |
| 63 | + arr: array of values ordered in ascending. |
| 64 | + target: target value to find in the array. |
| 65 | +
|
| 66 | + Returns: |
| 67 | + index of the target value if found, otherwise -1. |
| 68 | + """ |
| 69 | + if not arr: |
| 70 | + return -1 |
| 71 | + |
| 72 | + low = 0 |
| 73 | + high = len(arr) - 1 |
| 74 | + |
| 75 | + while low <= high: |
| 76 | + # Integer division, |
| 77 | + # doesn't need to worry about overflow |
| 78 | + # since python integers support arbitrary precision |
| 79 | + mid = (low + high) // 2 |
| 80 | + |
| 81 | + if arr[mid] == target: |
| 82 | + return mid |
| 83 | + elif arr[mid] < target: |
| 84 | + low = mid + 1 |
| 85 | + else: |
| 86 | + high = mid - 1 |
| 87 | + return -1 |
| 88 | +``` |
| 89 | + |
| 90 | +## A bug due to integer overflow was never |
| 91 | + |
| 92 | +The only interesting difference between the Java and Python version is how the `mid` value is calculated. In Java, one has to know that `int` supports only fixed precision hence it is buggy to do `(low + high)/2` since `(low + high)` can overflow for very high values of `low` and `high`. Read all about how this bug had lurked in JDK for over 9 years without anyone noticing - https://web.archive.org/web/20101202023037/http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html |
| 93 | + |
| 94 | +In python, since by default it supports arbitrary precision, the natural way to take an average of 2 numbers is also correct! |
| 95 | + |
| 96 | +## Outro |
| 97 | + |
| 98 | +In this post, I am not trying to compare Java and Python to show Java in negative light. It is to argue a general point that language semantics matter especially if the language syntax also makes it natural to write semantically correct code. In this particular case, I like the no-surprise syntax and semantics of Python when it comes to taking a mean of two values. |
0 commit comments