Skip to content

Commit 54cdbbb

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Miscellaneous: Flipping bits. Solved ✅.
1 parent 8945236 commit 54cdbbb

File tree

3 files changed

+228
-0
lines changed

3 files changed

+228
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# [Miscellaneous: Flipping bits](https://www.hackerrank.com/challenges/flipping-bits)
2+
3+
- Difficulty: `#easy`
4+
- Category: `#ProblemSolvingBasic` `#BitManipulation`
5+
6+
You will be given a list of 32 bit unsigned integers.
7+
Flip all the bits ( `1 -> 0` and `0 -> 1`)
8+
and return the result as an unsigned integer.
9+
10+
## Example
11+
12+
$ n = 9_{10} $. We're working with 32 bits, so:
13+
14+
$ 9_{10} = 1001_{2} $
15+
16+
$ 00000000000000000000000000001001_{2} = 9_{10} $
17+
$ 11111111111111111111111111110110_{2} = 4294967286_{10} $
18+
19+
Return `4294967286`
20+
21+
## Function Description
22+
23+
Complete the flippingBits function in the editor below.
24+
25+
flippingBits has the following parameter(s):
26+
27+
- `int n`: an integer
28+
29+
## Returns
30+
31+
- `int`: the unsigned decimal integer result
32+
33+
## Input Format
34+
35+
The first line of the input contains `q`, the number of queries.
36+
Each of the next `q` lines contain an integer, `n`, to process.
37+
38+
## Constraints
39+
40+
- $ 1 \leq q \leq 100 $
41+
- $ 0 \leq n \leq 2^{32} $
42+
43+
## Sample Input 0
44+
45+
```text
46+
3
47+
2147483647
48+
1
49+
0
50+
```
51+
52+
## Sample Output 0
53+
54+
```text
55+
2147483648
56+
4294967294
57+
4294967295
58+
```
59+
60+
## Explanation 0
61+
62+
$ 01111111111111111111111111111111_{2} = 2147483647_{10} $
63+
$ 10000000000000000000000000000000_{2} = 2147483648_{10} $
64+
65+
$ 00000000000000000000000000000001_{2} = 1_{10} $
66+
$ 11111111111111111111111111111110_{2} = 4294967294_{10} $
67+
68+
$ 00000000000000000000000000000000_{2} = 0_{10} $
69+
$ 11111111111111111111111111111110_{2} = 4294967295_{10} $
70+
71+
## Sample Input 1
72+
73+
```text
74+
2
75+
4
76+
123456
77+
```
78+
79+
## Sample Output 1
80+
81+
```text
82+
4294967291
83+
4294843839
84+
```
85+
86+
## Explanation 1
87+
88+
$ 00000000000000000000000000000100_{2} = 4_{10} $
89+
$ 11111111111111111111111111111011_{2} = 4294967291_{10} $
90+
91+
$ 00000000000000011110001001000000_{2} = 4_{10} $
92+
$ 11111111111111100001110110111111_{2} = 429484389_{10} $
93+
94+
## Sample Input 2
95+
96+
```text
97+
3
98+
0
99+
802743475
100+
35601423
101+
```
102+
103+
## Sample Output 2
104+
105+
```text
106+
4294967295
107+
3492223820
108+
4259365872
109+
```
110+
111+
## Explanation 2
112+
113+
$ 00000000000000000000000000000000_{2} = 4_{10} $
114+
$ 11111111111111111111111111111111_{2} = 4294967295_{10} $
115+
116+
$ 00101111110110001110010010110011_{2} = 802743475_{10} $
117+
$ 11010000001001110001101101001100_{2} = 3492223820_{10} $
118+
119+
$ 00000010000111110011110000001111_{2} = 35601423_{10} $
120+
$ 11111101111000001100001111110000_{2} = 4259365872_{10} $
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
import { logger as console } from '../../../logger';
3+
4+
import { flippingBits } from './flipping-bits';
5+
6+
const TEST_CASES = [
7+
{
8+
title: 'Sample Test Case 0',
9+
tests: [
10+
{
11+
input: 2147483647,
12+
expected: 2147483648
13+
},
14+
{
15+
input: 1,
16+
expected: 4294967294
17+
},
18+
{
19+
input: 0,
20+
expected: 4294967295
21+
}
22+
]
23+
},
24+
{
25+
title: 'Sample Test Case 1',
26+
tests: [
27+
{
28+
input: 4,
29+
expected: 4294967291
30+
},
31+
{
32+
input: 123456,
33+
expected: 4294843839
34+
}
35+
]
36+
},
37+
{
38+
title: 'Sample Test Case 2',
39+
tests: [
40+
{
41+
input: 0,
42+
expected: 4294967295
43+
},
44+
{
45+
input: 802743475,
46+
expected: 3492223820
47+
},
48+
{
49+
input: 35601423,
50+
expected: 4259365872
51+
}
52+
]
53+
}
54+
];
55+
56+
describe('flipping bits', () => {
57+
it('flipping bits test cases', () => {
58+
expect.assertions(8);
59+
60+
TEST_CASES.forEach((test_set) => {
61+
test_set.tests.forEach((test) => {
62+
const answer = flippingBits(test.input);
63+
64+
console.debug(`luckBalance(${test.input}) solution found: ${answer}`);
65+
66+
expect(answer).toStrictEqual(test.expected);
67+
});
68+
});
69+
});
70+
71+
it('flipping bits isolated test case', () => {
72+
expect.assertions(1);
73+
74+
const input = 9;
75+
const expected = 4294967286;
76+
77+
const answer = flippingBits(input);
78+
79+
console.debug(`luckBalance(${input}) solution found: ${answer}`);
80+
81+
expect(answer).toStrictEqual(expected);
82+
});
83+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/miscellaneous/flipping-bits.md]]
3+
*/
4+
5+
const __BINARY_BASE__ = 2;
6+
const __NUMBER_SIZE_IN_BITS__ = 32;
7+
8+
export function flippingBits(n: number): number {
9+
let n_binary_str = n.toString(__BINARY_BASE__);
10+
n_binary_str = n_binary_str.padStart(__NUMBER_SIZE_IN_BITS__, '0');
11+
12+
let result_bin_str = '';
13+
14+
n_binary_str.split('').forEach((bin_digit) => {
15+
if (bin_digit == '1') {
16+
result_bin_str += '0';
17+
} else {
18+
result_bin_str += '1';
19+
}
20+
});
21+
22+
return parseInt(result_bin_str, __BINARY_BASE__);
23+
}
24+
25+
export default { flippingBits };

0 commit comments

Comments
 (0)