LeetCode Çözümlerİ – 202. Happy Number – [Google Mülakat Sorusu]

LeetCode içerisinde bulunan “Happy Number”‘ sorusunun açıklaması ve çözümü. Bu soruda bir sayının rakamlarının karelerini toplayıp ve bu işlemi sonsuz döngü olmadığı sürece devam ettirip 1 sayısına ulaşılabiliyor mu olduğu sormakta. LeetCode’a göre Google iş görüşmelerinde sorulmuş sorulardan biriymiş.

🔥 LeetCode 202. Happy Number: https://leetcode.com/problems/happy-number/

➡️ Problem açıklaması:

Write an algorithm to determine if a number n is “happy”.

A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.

Return True if n is a happy number, and False if not.

Example:

Input: 19

Output: true

Explanation:

1^2 + 9^2 = 82

8^2 + 2^2 = 68

6^2 + 8^2 = 100

1^2 + 0^2 + 0^2 = 1

➡️ Wikipedia açıklaması: https://en.wikipedia.org/wiki/Happy_number

In number theory, a happy number is a number which eventually reaches 1 when replaced by the sum of the square of each digit. For instance, 13 is a happy number because 1^{2}+3^{2}=10 and 1^{2}+0^{2}=1. On the other hand, 4 is not a happy number because the sequence starting with 4^{2}=16 and 1^{2}+6^{2}=37 eventually reaches 2^{2}+0^{2}=4, the number that started the sequence, and so the process continues in an infinite cycle without ever reaching 1. A number which is not happy is called sad or unhappy.

LeetCode Çözümleri – 326. Power of Three

LeetCode içerisinde bulunan “Power of Three”‘ sorusunun açıklaması ve çözümü. Soruda verilen integer sayısının 3’ün bir tam sayı kuvveti olup olmadığı sorulmaktadır.

🔥 LeetCode 326. Power of Three: https://leetcode.com/problems/power-of-three/

➡️ Problem açıklaması:

Given an integer, write a function to determine if it is a power of three.

Example 1:

Input: 27

Output: true

Example 2:

Input: 0

Output: false

Example 3:

Input: 9

Output: true

Example 4:

Input: 45

Output: false

Follow up: Could you do it without using any loop / recursion?

😔 NOT: Mikrofonda bazı sorunlar yaşanmış kayıt yaparken, kusura bakmayın.

LeetCode Çözümlerİ – 268. Missing Number [Google ve Amazon Mülakat Sorusu]

LeetCode içerisinde bulunan “Missing Number”‘ sorusunun açıklaması ve çözümü. Bu soruda [0..N] dizisinde verilen N adet sayıdan eksik olanı bulmanız isteniyor. LeetCode’a göre Google ve Amazon iş görüşmelerinde sorular sorulardan biri. Videoda alternatif XOR çözümünü de anlatmaya çalıştım.

🔥 LeetCode 268. Missing Number: https://leetcode.com/problems/missing-number/

➡️ Problem açıklaması:

Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array.

Example 1:

Input: [3,0,1]

Output: 2

Example 2:

Input: [9,6,4,2,3,5,7,0,1]

Output: 8 Note:

Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?

LeetCode Çözümlerİ – 231. Power of Two [Amazon Mülakat Sorusu]

LeetCode içerisinde bulunan “Power of Two”‘ sorusunun açıklaması ve çözümü. Bu soruda verilen sayının 2’nin pozitif bir tam kuvveti olup olmadığını bulmanız isteniyor. LeetCode’a göre Amazon’da sorulmuş sorulardan biri.

➡️ LeetCode 231. Power of Two: https://leetcode.com/problems/power-of-two/

➡️ Bit Twiddling Hacks: http://graphics.stanford.edu/~seander/bithacks.html

➡️ the bit twiddler: https://bits.stephan-brumme.com/

➡️ The Aggregate Magic Algorithms: http://aggregate.org/MAGIC

➡️ Bit Tricks: https://medium.com/trick-the-interviwer/bit-tricks-577ebb2f1a8b

➡️ Basics of Bit Manipulation: https://www.hackerearth.com/practice/basic-programming/bit-manipulation/basics-of-bit-manipulation/tutorial/

➡️ Problem açıklaması:

Given an integer, write a function to determine if it is a power of two.

Example 1:

Input: 1

Output: true

Explanation: 2^0 = 1

Example 2:

Input: 16

Output: true

Explanation: 2^4 = 16

Example 3:

Input: 218

Output: false

LeetCode Çözümlerİ – 168. Excel Sheet Column Title

LeetCode içerisinde bulunan “Excel Sheet Column Title”‘ sorusunun açıklaması ve çözümü.

🔥 LeetCode 88. Merge Sorted Array: https://leetcode.com/problems/excel-sheet-column-title/

➡️ Problem açıklaması:

Given a positive integer, return its corresponding column title as appear in an Excel sheet.

For example:

1 — A

2 — B

3 — C

26 — Z

27 — AA

28 — AB

Example 1:

Input: 1

Output: “A”

Example 2:

Input: 28

Output: “AB”

Example 3:

Input: 701

Output: “ZY”

LeetCode Çözümlerİ – 171. Excel Sheet Column Number

LeetCode içerisinde bulunan “Excel Sheet Column Number”‘ sorusunun açıklaması ve çözümü. Bu soruda verilen excel kolon isminden, kaçıncı kolon olduğunu bulmanız gerekiyor.

🔥 LeetCode 171. Excel Sheet Column Number: https://leetcode.com/problems/excel-sheet-column-number/

➡️ Problem açıklaması:

Given a column title as appear in an Excel sheet, return its corresponding column number.

For example:

A — 1

B — 2

C — 3

Z — 26

AA — 27

AB — 28

Example 1:

Input: “A”

Output: 1

Example 2:

Input: “AB”

Output: 28

Example 3:

Input: “ZY”

Output: 701

Constraints: 1 less= s.length less= 7 s consists only of uppercase English letters. s is between “A” and “FXSHRXW”.

LeetCode Çözümlerİ – 175. Combine Two Tables

LeetCode içerisinde bulunan “Combine Two Tables”‘ sorusunun açıklaması ve çözümü.

➡️ LeetCode 175. Combine Two Tables: https://leetcode.com/problems/combine-two-tables/

Problem açıklaması:

Table: Person

+————-+———+

| Column Name | Type |

+————-+———+

| PersonId | int |

| FirstName | varchar |

| LastName | varchar |

+————-+———+

PersonId is the primary key column for this table.

Table: Address

+————-+———+

| Column Name | Type |

+————-+———+

| AddressId | int |

| PersonId | int |

| City | varchar |

| State | varchar |

+————-+———+

AddressId is the primary key column for this table.

Write a SQL query for a report that provides the following information for each person in the Person table, regardless if there is an address for each of those people:

FirstName, LastName, City, State

LeetCode Çözümlerİ – 181. Employees Earning More Than Their Managers

LeetCode içerisinde bulunan “Employees Earning More Than Their Managers”‘ sorusunun açıklaması ve çözümü.

🔥 LeetCode 181. Employees Earning More Than Their Managers: https://leetcode.com/problems/employees-earning-more-than-their-managers/

➡️ Problem açıklaması:

The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.

+—-+——-+——–+———–+

| Id | Name | Salary | ManagerId |

+—-+——-+——–+———–+

| 1 | Joe | 70000 | 3 |

| 2 | Henry | 80000 | 4 |

| 3 | Sam | 60000 | NULL |

| 4 | Max | 90000 | NULL |

+—-+——-+——–+———–+

Given the Employee table, write a SQL query that finds out employees who earn more than their managers. For the above table, Joe is the only employee who earns more than his manager.

+———-+

| Employee |

+———-+

| Joe |

+———-+

LeetCode Çözümlerİ – 183. Customers Who Never Order

LeetCode içerisinde bulunan “Customers Who Never Order”‘ sorusunun açıklaması ve çözümü.

🔥 LeetCode 183. Customers Who Never Order: https://leetcode.com/problems/customers-who-never-order/

➡️ Problem açıklaması:

Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL query to find all customers who never order anything.

Table: Customers.

+—-+——-+

| Id | Name |

+—-+——-+

| 1 | Joe |

| 2 | Henry |

| 3 | Sam |

| 4 | Max |

+—-+——-+

Table: Orders.

+—-+————+

| Id | CustomerId |

+—-+————+

| 1 | 3 |

| 2 | 1 |

+—-+————+

Using the above tables as example, return the following:

+———–+

| Customers |

+———–+

| Henry |

| Max |

+———–+

LeetCode Çözümlerİ – 176. Second Highest Salary

LeetCode içerisinde bulunan “Second Highest Salary”‘ sorusunun açıklaması ve çözümü.

🔥 LeetCode 176. Second Highest Salary: https://leetcode.com/problems/second-highest-salary/

➡️ Problem açıklaması:

Write a SQL query to get the second highest salary from the Employee table.

+—-+——–+

| Id | Salary |

+—-+——–+

| 1 | 100 |

| 2 | 200 |

| 3 | 300 |

+—-+——–+

For example, given the above Employee table, the query should return 200 as the second highest salary. If there is no second highest salary, then the query should return null.

+———————+

| SecondHighestSalary |

+———————+

| 200 |

+———————+