Last active
February 25, 2024 01:24
-
-
Save HahaBill/640f55c8aebee623c482db614adb29dc to your computer and use it in GitHub Desktop.
Interview Question from AppBrewery - Ton Hoang Nguyen (Bill)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
The problem can be solve using two pointers: | |
a. one (l) pointing to the first element | |
b. the other one (r) pointing to the last element | |
Then in a while loop, we iterate and compute the area | |
by computing a distance (r - l) and taking the height | |
of the building that is less tall. We then compare | |
the temporary current result with the largest area | |
(max). | |
In a loop, we look at the area between l and r. | |
We do this by finding the distance between l and r and | |
using the height of the shorter building. | |
We keep track of the biggest area we find. | |
We keep moving l and r closer together until they meet. | |
In the end, we have found the biggest area possible | |
and return it. | |
*/ | |
export function maxArea(height: number[]): number { | |
let l = 0, r = height.length - 1, max = 0; | |
while (l < r) { | |
max = Math.max(max, (r-l) * Math.min(height[l], height[r])); | |
if (height[l] <= height[r]) { | |
l++; | |
} else { | |
r--; | |
} | |
} | |
return max; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { maxArea } from '../src/computeMaxArea' | |
describe('maxArea', () => { | |
test('should return 1', () => { | |
expect(maxArea([1, 1])).toBe(1); | |
}); | |
test('should return 49', () => { | |
expect(maxArea([1, 8, 6, 2, 5, 4, 8, 3, 7])).toBe(49); | |
}); | |
test('should return 0 if there is only one building', () => { | |
expect(maxArea([1, 0, 0, 0, 0])).toBe(0); | |
}); | |
test('should return 8 if they are only two building at both ends', () => { | |
expect(maxArea([5, 0, 0, 0, 2])).toBe(8); | |
}); | |
test('should return 8 if there are only two adjacent buildings', () => { | |
expect(maxArea([0, 0, 8, 8, 0, 0])).toBe(8); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(base) billtonhoang@BillTonHoangs-MacBook-Pro compute-max-area % npm run test | |
> [email protected] test | |
> node --experimental-vm-modules node_modules/.bin/jest | |
jest-haste-map: Watchman crawl failed. Retrying once with node crawler. | |
Usually this happens when watchman isn't running. Create an empty `.watchmanconfig` file in your project's root folder or initialize a git or hg repository in your project. | |
Error: Watchman error: std::__1::system_error: open: /Users/billtonhoang/Documents/GitHub/compute-max-area: Operation not permitted. Make sure watchman is running for this project. See https://facebook.github.io/watchman/docs/troubleshooting. | |
(node:51889) ExperimentalWarning: VM Modules is an experimental feature. This feature could change at any time | |
(Use `node --trace-warnings ...` to show where the warning was created) | |
PASS __test__/computeMaxArea.test.ts | |
maxArea | |
✓ should return 1 (2 ms) | |
✓ should return 49 (1 ms) | |
✓ should return 0 if there is only one building | |
✓ should return 8 if they are only two building at both ends | |
✓ should return 8 if there are only two adjacent buildings | |
Test Suites: 1 passed, 1 total | |
Tests: 5 passed, 5 total | |
Snapshots: 0 total | |
Time: 1.365 s, estimated 2 s | |
Ran all test suites. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment