Categories

# Kotlin for Interviews — Part 3: Numbers and Math | by Sherry Yuan

This is Part 3 of Kotlin for Interviews. This series describes Kotlin’s functions and code snippets that came up frequently while preparing for an Android interview. We have also created a cheat sheet that covers all five parts of this series. Here..

Part 1: You can find common data types Here, Part 2: Collection function Here And Part 4: Iteration Here And Part 5: Frequently used code snippets Here..

The contents of this part are as follows:

Some of the math interview questions require a deeper understanding of Kotlin numbers and math operations than you need at work, so this section will cover them.

You can find a more detailed overview in Kotlin Basic type document..

• `Int` It is a number without a decimal point. The range is -2³¹ to 2³¹-1, which is stored in 32 bits.
• `Long` This is also a number without a decimal point, but it is stored in the range -2⁶³ to 2⁶³-1 in 64-bit, so it can be used when you need to store a larger number.
• `Float` Is a floating point number that can accurately represent a 6-7 digit decimal number. That is, if you see a float of 0.12345678, you get an accuracy of up to 0.123457, and if you see 1234.5678, you get an accuracy of up to 1234.57. It is stored in 32 bits, 24 bits for the radix and 8 bits for the exponent.
• `Double` Is a floating point number that can accurately represent a decimal number with 10 to 16 digits. It is saved in 64-bit. Decimal number `Float` Or `Double`, Kotlin initializes it as follows `Double` By default.

Here’s how to initialize each type:

`val oneInt = 1 // Intval oneLong = 1L // Longval oneFloat = 1f // Floatval oneDouble = 1.0 // Double// Note: simply specifying that a number is a Float or a Double, for example // val oneFloat: Float = 1 // val oneDouble: Double = 1// does NOT work. You’ll get a "The integer literal does not conform // to the expected type Float/Double" error if you don’t append the f or .0.// However you can declare a Long without appending the L.// val oneLong: Long = 1// is fine.`

Kotlin uses the symbols you would expect from most operators — `+` further, `—` For subtraction `*` In the case of multiplication `/` In the case of division `%` For mods — so I won’t talk about them.

However, Kotlin’s index is a bit complicated.You need to use `Double.pow()` Or `Float.pow()` From `kotlin.math` library.There is none `Int.pow()`, So you `Int` You need to cast it to `Double` the first.

`// pow() isn't in the standard library, so you'd have to explicitly // add this import.import kotlin.math.pow3.0.pow(4) // returns 81.0 as a Double3.toDouble().pow(4) // also returns 81.0 as a Double// Note: 3.pow(4) does NOT work and will give you an "Unresolved // reference error", since there's no Int.pow() in kotlin.math.`

Includes sample code for math functions that you had to use often.You can see the complete list of features Here..

• `abs()` Returns the absolute value of the specified value.
• `max()` Returns the larger of the two specified values.
• `ceil()` Rounds the specified value to the closest integer that is larger than itself.
• `floor()` Rounds the specified value to the nearest integer that is smaller than itself.
• `round()` Rounds the specified value to the nearest integer.
• `sqrt()` Returns the positive square root of the specified value.
`// Absolute valueMath.abs(-1) // returns 1// MaxMath.max(1, 3) // returns 3// MinMath.min(1, 3) // returns 1// CeilingMath.ceil(2.2) // returns 3.0// FloorMath.floor(2.8) // returns 2.0// RoundingMath.round(2.5) // returns 3Math.round(1.5) // returns 2Math.round(1.4) // returns 1Math.round(2.6) // returns 3// Square rootMath.sqrt(6.25) // returns 2.5`

Alternatively, you can import it explicitly kotlin.math Package and use the fields and functions it provides. `Math.` Prefix:

`import kotlin.math.*// Absolute valueabs(-1) // returns 1-1.absoluteValue // also returns 1// Maxmax(1, 3) // returns 3// Minmin(1, 3) // returns 1// Ceilingceil(2.2) // returns 3.0// Floorfloor(2.8) // returns 2.0// Roundinground(2.5) // returns 32.5.roundToInt() // also returns 3// Square rootsqrt(6.25) // returns 2.5`

To Kotlin `MAX_VALUE` And `MIN_VALUE` A constant for each number type.

One pitfall is `MIN_VALUE` It is different for integers and floating point numbers.

• `Int.MIN_VALUE` Holds the minimum value of the instance `Int` Also known as the largest negative value you can have.
• `Float.MIN_VALUE` Keep the minimum positive Non-zero value of `Float`..

If you need the maximum possible negative value for `Float`, Must be used `-1 * Float.MAX_VALUE`..

`Int.MAX_VALUE // 2147483647Int.MIN_VALUE // -2147483648Long.MAX_VALUE // 9223372036854775807Long.MIN_VALUE // -9223372036854775808Float.MAX_VALUE // 3.4028235E38Float.MIN_VALUE // 1.4E-45-1 * Float.MAX_VALUE // -3.4028235E38Double.MAX_VALUE // 1.7976931348623157E308Double.MIN_VALUE // 4.9E-324-1 * Double.MAX_VALUE // -1.7976931348623157E308`

These are useful for problems where you need to keep track of the minimum or maximum numbers you’ve ever seen in a collection and need to initialize that tracker with something.

For example, find the maximum number in the list:

`var maxSoFar = Int.MIN_VALUEval numbers: List<Int> = listOf(3, -1, 0, 3352)numbers.forEach { number ->if (number > maxSoFar) {maxSoFar = number} }`

This is a trivial example you can just use `numbers.max()` You get the same result, but you get the idea.

This is Link to cheat sheet Cover all five parts again.

Part 4 of the coding interview cracking covers the iterations and is published Here..

Source