Skip to content

Commit

Permalink
[Hacker Rank] Interview Preparation Kit: Miscellaneous: Flipping bits…
Browse files Browse the repository at this point in the history
…. Solved ✅.
  • Loading branch information
Gonzalo Diaz committed Jul 16, 2024
1 parent 8945236 commit 8c89108
Show file tree
Hide file tree
Showing 3 changed files with 228 additions and 0 deletions.
120 changes: 120 additions & 0 deletions docs/hackerrank/miscellaneous/flipping-bits.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# [Miscellaneous: Flipping bits](https://www.hackerrank.com/challenges/flipping-bits)

- Difficulty: `#easy`
- Category: `#ProblemSolvingBasic` `#BitManipulation`

You will be given a list of 32 bit unsigned integers.
Flip all the bits ( `1 -> 0` and `0 -> 1`)
and return the result as an unsigned integer.

## Example

$ n = 9_{10} $. We're working with 32 bits, so:

$ 9_{10} = 1001_{2} $

$ 00000000000000000000000000001001_{2} = 9_{10} $
$ 11111111111111111111111111110110_{2} = 4294967286_{10} $

Return `4294967286`

## Function Description

Complete the flippingBits function in the editor below.

flippingBits has the following parameter(s):

- `int n`: an integer

## Returns

- `int`: the unsigned decimal integer result

## Input Format

The first line of the input contains `q`, the number of queries.
Each of the next `q` lines contain an integer, `n`, to process.

## Constraints

- $ 1 \leq q \leq 100 $
- $ 0 \leq n \leq 2^{32} $

## Sample Input 0

```text
3
2147483647
1
0
```

## Sample Output 0

```text
2147483648
4294967294
4294967295
```

## Explanation 0

$ 01111111111111111111111111111111_{2} = 2147483647_{10} $
$ 10000000000000000000000000000000_{2} = 2147483648_{10} $

$ 00000000000000000000000000000001_{2} = 1_{10} $
$ 11111111111111111111111111111110_{2} = 4294967294_{10} $

$ 00000000000000000000000000000000_{2} = 0_{10} $
$ 11111111111111111111111111111110_{2} = 4294967295_{10} $

## Sample Input 1

```text
2
4
123456
```

## Sample Output 1

```text
4294967291
4294843839
```

## Explanation 1

$ 00000000000000000000000000000100_{2} = 4_{10} $
$ 11111111111111111111111111111011_{2} = 4294967291_{10} $

$ 00000000000000011110001001000000_{2} = 4_{10} $
$ 11111111111111100001110110111111_{2} = 429484389_{10} $

## Sample Input 2

```text
3
0
802743475
35601423
```

## Sample Output 2

```text
4294967295
3492223820
4259365872
```

## Explanation 2

$ 00000000000000000000000000000000_{2} = 4_{10} $
$ 11111111111111111111111111111111_{2} = 4294967295_{10} $

$ 00101111110110001110010010110011_{2} = 802743475_{10} $
$ 11010000001001110001101101001100_{2} = 3492223820_{10} $

$ 00000010000111110011110000001111_{2} = 35601423_{10} $
$ 11111101111000001100001111110000_{2} = 4259365872_{10} $
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { describe, expect, it } from '@jest/globals';
import { logger as console } from '../../../logger';

import { flippingBits } from './flipping-bits';

const TEST_CASES = [
{
title: 'Sample Test Case 0',
tests: [
{
input: 2147483647,
expected: 2147483648
},
{
input: 1,
expected: 4294967294
},
{
input: 0,
expected: 4294967295
}
]
},
{
title: 'Sample Test Case 1',
tests: [
{
input: 4,
expected: 4294967291
},
{
input: 123456,
expected: 4294843839
}
]
},
{
title: 'Sample Test Case 2',
tests: [
{
input: 0,
expected: 4294967295
},
{
input: 802743475,
expected: 3492223820
},
{
input: 35601423,
expected: 4259365872
}
]
}
];

describe('flipping bits', () => {
it('flipping bits test cases', () => {
expect.assertions(8);

TEST_CASES.forEach((test_set) => {
test_set.tests.forEach((test) => {
const answer = flippingBits(test.input);

console.debug(`luckBalance(${test.input}) solution found: ${answer}`);

expect(answer).toStrictEqual(test.expected);
});
});
});

it('flipping bits isolated test case', () => {
expect.assertions(1);

const input = 9;
const expected = 4294967286;

const answer = flippingBits(input);

console.debug(`luckBalance(${input}) solution found: ${answer}`);

expect(answer).toStrictEqual(expected);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @link Problem definition [[docs/hackerrank/miscellaneous/flipping-bits.md]]
*/

const __BINARY_BASE__ = 2;
const __NUMBER_SIZE_IN_BITS__ = 32;

export function flippingBits(n: number): number {
let n_binary_str = n.toString(__BINARY_BASE__);
n_binary_str = n_binary_str.padStart(__NUMBER_SIZE_IN_BITS__, '0');

let result_bin_str = '';

n_binary_str.split('').forEach((bin_digit) => {
if (bin_digit == '1') {
result_bin_str = result_bin_str + '0';
} else {
result_bin_str = result_bin_str + '1';
}
});

return parseInt(result_bin_str, __BINARY_BASE__);
}

export default { flippingBits };

0 comments on commit 8c89108

Please sign in to comment.