Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
0
of 0 vote

(n & (1 << k)) ? (n ^ (1 << k)) : n

- Nisheeth August 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

(n & (1 << k)) ? (n ^ (1 << k)): n

- nisheethbhat August 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

(~(1 << k)) & n

- Anonymous August 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

n & (!(1 << k))

- Ronen August 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Let's keep this example at 8 bits for simplicity.

Let's start with our sample input: input = 10101010
Create another number i which will start at 1: i = 00000001
Shift it k times. (Let's say k = 3): i = 00001000
Negate it with XOR by performing i ^ Int.max: i = i ^ 11111111 = 11110111
Return i & number: 10100010

Swift 2.2

func flip(k: Int, number:Int) -> String {
    var operand = 1
    operand = operand << k
    operand = operand ^ Int.max
    return String(operand & number, radix: 2)
    
}

flip(0, number: 0b101) // "100"
flip(1, number: 0b101) // "101"
flip(2, number: 0b101) // "1"

- helloru August 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

(n & (1<<k)) ? (n - (1 << k)) : n

- eevee August 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

(n & (1 << k)) ? (n - (1 << k)): n

- eevee August 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

isnt it just:

$n ^ (1 << $k) ?

- pyromancer August 08, 2016 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More