You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
3.1 KiB
79 lines
3.1 KiB
6 years ago
|
---
|
||
|
id: binary
|
||
3 years ago
|
title: Binary cheatsheet for coding interviews
|
||
|
description: binary study guide for coding interviews, including practice questions, techniques, time complexity, and recommended resources
|
||
|
keywords:
|
||
|
[
|
||
|
binary coding interview study guide,
|
||
|
binary tips for coding interviews,
|
||
|
binary practice questions,
|
||
|
binary useful techniques,
|
||
|
binary time complexity,
|
||
|
binary recommended study resources,
|
||
|
]
|
||
|
sidebar_label: Binary
|
||
3 years ago
|
toc_max_heading_level: 2
|
||
6 years ago
|
---
|
||
7 years ago
|
|
||
3 years ago
|
<head>
|
||
|
<meta property="og:image" content="https://www.techinterviewhandbook.org/social/algorithms/algorithms/algorithms-binary.png" />
|
||
|
</head>
|
||
|
|
||
3 years ago
|
## Introduction
|
||
6 years ago
|
|
||
3 years ago
|
Knowledge of binary number system and bit manipulation is less important in coding interviews as most Software Engineers do not have to deal with bits, which is more commonly used when dealing with lower level systems and programming languages. They are still asked sometimes, so you should at least still know how to convert a number from decimal form into binary form, and vice versa, in your chosen programming language.
|
||
6 years ago
|
|
||
3 years ago
|
## Learning resources
|
||
|
|
||
|
- Readings
|
||
|
- [Bits, Bytes, Building With Binary](https://medium.com/basecs/bits-bytes-building-with-binary-13cb4289aafa), basecs
|
||
|
- [Bitwise operation](https://en.wikipedia.org/wiki/Bitwise_operation), Wikipedia
|
||
|
- Videos
|
||
|
- [Algorithms: Bit Manipulation](https://www.youtube.com/watch?v=NLKQEOgBAnw), HackerRank
|
||
|
- Practice
|
||
|
- [Practice with bit operations](https://pconrad.github.io/old_pconrad_cs16/topics/bitOps/)
|
||
|
|
||
6 years ago
|
## Corner cases
|
||
|
|
||
|
- Be aware and check for overflow/underflow
|
||
|
- Negative numbers
|
||
|
|
||
3 years ago
|
## Techniques
|
||
|
|
||
|
Questions involving binary representations and bitwise operations are asked sometimes and you must be absolutely familiar with how to convert a number from decimal form into binary form (and vice versa) in your chosen programming language.
|
||
|
|
||
|
Some helpful utility snippets:
|
||
|
|
||
|
| Technique | Code |
|
||
|
| --- | --- |
|
||
|
| Test k<sup>th</sup> bit is set | `num & (1 << k) != 0`. |
|
||
|
| Set k<sup>th</sup> bit | <code>num |= (1 << k)</code> |
|
||
|
| Turn off k<sup>th</sup> bit | `num &= ~(1 << k)`. |
|
||
|
| Toggle the k<sup>th</sup> bit | `num ^= (1 << k)`. |
|
||
3 years ago
|
| Multiply by 2<sup>k</sup> | `num << k` |
|
||
3 years ago
|
| Divide by 2<sup>k</sup> | `num >> k` |
|
||
|
| Check if a number is a power of 2 | `(num & num - 1) == 0` or `(num & (-num)) == num` |
|
||
|
| Swapping two variables | `num1 ^= num2; num2 ^= num1; num1 ^= num2` |
|
||
|
|
||
3 years ago
|
## Essential questions
|
||
|
|
||
|
_These are essential questions to practice if you're studying for this topic._
|
||
6 years ago
|
|
||
|
- [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/)
|
||
|
- [Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/)
|
||
3 years ago
|
|
||
|
## Recommended practice questions
|
||
|
|
||
|
_These are recommended questions to practice after you have studied for the topic and have practiced the essential questions._
|
||
|
|
||
6 years ago
|
- [Counting Bits](https://leetcode.com/problems/counting-bits/)
|
||
|
- [Missing Number](https://leetcode.com/problems/missing-number/)
|
||
|
- [Reverse Bits](https://leetcode.com/problems/reverse-bits/)
|
||
3 years ago
|
- [Single Number](https://leetcode.com/problems/single-number/)
|
||
3 years ago
|
|
||
|
## Recommended courses
|
||
|
|
||
|
import AlgorithmCourses from '../\_courses/AlgorithmCourses.md'
|
||
|
|
||
|
<AlgorithmCourses />
|