Skip to content

Commit cd771b6

Browse files
authored
Merge pull request #356 from sir-gon/feature/flipping_bits
[Hacker Rank] Interview Preparation Kit: Miscellaneous: Flipping bits…
2 parents 3cbe120 + 88815f6 commit cd771b6

File tree

4 files changed

+232
-0
lines changed

4 files changed

+232
-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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/miscellaneous/flipping-bits.md]]
3+
*/
4+
5+
package hackerrank
6+
7+
func flippingBits(n int64) int64 {
8+
return ^n & 0xFFFFFFFF // Flip all bits and mask to 32 bits
9+
}
10+
11+
func FlippingBits(n int64) int64 {
12+
return flippingBits(n)
13+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
[
2+
{
3+
"title": "Sample Test Case 0",
4+
"tests": [
5+
{
6+
"input": 2147483647,
7+
"answer": 2147483648
8+
},
9+
{
10+
"input": 1,
11+
"answer": 4294967294
12+
},
13+
{
14+
"input": 0,
15+
"answer": 4294967295
16+
}
17+
]
18+
},
19+
{
20+
"title": "Sample Test Case 1",
21+
"tests": [
22+
{
23+
"input": 4,
24+
"answer": 4294967291
25+
},
26+
{
27+
"input": 123456,
28+
"answer": 4294843839
29+
}
30+
]
31+
},
32+
{
33+
"title": "Sample Test Case 2",
34+
"tests": [
35+
{
36+
"input": 0,
37+
"answer": 4294967295
38+
},
39+
{
40+
"input": 802743475,
41+
"answer": 3492223820
42+
},
43+
{
44+
"input": 35601423,
45+
"answer": 4259365872
46+
}
47+
]
48+
}
49+
]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package hackerrank
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"gon.cl/algorithms/utils"
10+
)
11+
12+
type FlipingBitsTest struct {
13+
Input int64 `json:"input"`
14+
Expected int64 `json:"answer"`
15+
}
16+
17+
type FlipingBitsTests struct {
18+
Title string `json:"title"`
19+
Tests []FlipingBitsTest `json:"tests"`
20+
}
21+
22+
var FlipingBitsTestCases []FlipingBitsTests
23+
24+
// You can use testing.T, if you want to test the code without benchmarking
25+
func FlipingBitsSetupSuite(t testing.TB) {
26+
wd, _ := os.Getwd()
27+
filepath := wd + "/flipping_bits.testcases.json"
28+
t.Log("Setup test cases from JSON: ", filepath)
29+
30+
var _, err = utils.LoadJSON(filepath, &FlipingBitsTestCases)
31+
if err != nil {
32+
t.Log(err)
33+
}
34+
}
35+
36+
func TestFlipingBits(t *testing.T) {
37+
38+
FlipingBitsSetupSuite(t)
39+
40+
for _, tt := range FlipingBitsTestCases {
41+
for _, testCase := range tt.Tests {
42+
testname := fmt.Sprintf("FlipingBits(%d) => %d \n", testCase.Input, testCase.Expected)
43+
44+
t.Run(testname, func(t *testing.T) {
45+
ans := FlippingBits(testCase.Input)
46+
assert.Equal(t, testCase.Expected, ans)
47+
})
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)