Microsoft Mülakat Sorusu – Intersection of Two Linked Lists

LeetCode içerisinde bulunan “Intersection of Two Linked Lists” sorusunun açıklaması ve çözümü. Bu soruda sizi verilen iki adet linked list‘in kesiştiği bir node var mı, varsa o node’u yoksa geriye null döndürmeniz isteniyor.

► LeetCode 160. Intersection of Two Linked Lists: https://leetcode.com/problems/intersection-of-two-linked-lists/

► Problem açıklaması:

Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.

For example, the following two linked lists begin to intersect at node c1:

The test cases are generated such that there are no cycles anywhere in the entire linked structure.

Note that the linked lists must retain their original structure after the function returns.

Custom Judge:

The inputs to the judge are given as follows (your program is not given these inputs):

intersectVal – The value of the node where the intersection occurs. This is 0 if there is no intersected node.

listA – The first linked list.

listB – The second linked list.

skipA – The number of nodes to skip ahead in listA (starting from the head) to get to the intersected node.

skipB – The number of nodes to skip ahead in listB (starting from the head) to get to the intersected node.

The judge will then create the linked structure based on these inputs and pass the two heads, headA and headB to your program. If you correctly return the intersected node, then your solution will be accepted.

Follow up: Could you write a solution that runs in O(n) time and use only O(1) memory?

LeetCode Çözümleri – Determine if String Halves Are Alike

LeetCode içerisinde bulunan “Determine if String Halves Are Alike” sorusunun açıklaması ve çözümü. Bu soruda sizi verilen bir string‘in ilk ve ikinci yarısındaki ünlü harf sayılarının eşit olup olmadığını bulmanız isteniyor.

► LeetCode 1704. Determine if String Halves Are Alike: https://leetcode.com/problems/determine-if-string-halves-are-alike/

► Problem açıklaması:

You are given a string s of even length. Split this string into two halves of equal lengths, and let a be the first half and b be the second half.

Two strings are alike if they have the same number of vowels (‘a’, ‘e’, ‘i’, ‘o’, ‘u’, ‘A’, ‘E’, ‘I’, ‘O’, ‘U’). Notice that s contains uppercase and lowercase letters.

Return true if a and b are alike. Otherwise, return false.

Example 1:

Input: s = “book”

Output: true

Explanation: a = “bo” and b = “ok”. a has 1 vowel and b has 1 vowel. Therefore, they are alike.

Example 2:

Input: s = “textbook”

Output: false

Explanation: a = “text” and b = “book”. a has 1 vowel whereas b has 2. Therefore, they are not alike. Notice that the vowel o is counted twice.

Example 3:

Input: s = “MerryChristmas”

Output: false

Example 4:

Input: s = “AbCdEfGh”

Output: true

Constraints:

2 <= s.length <= 1000

s.length is even.

s consists of uppercase and lowercase letters.

LeetCode Çözümleri – Consecutive Characters

LeetCode içerisinde bulunan “Consecutive Characters” sorusunun açıklaması ve çözümü. Bu soruda sizi verilen bir string içerisinde, yan yana olan aynı karakterlerin maksimum sayısını bulmanız isteniyor.

► LeetCode 1446. Consecutive Characters: https://leetcode.com/problems/consecutive-characters/

► Problem açıklaması:

The power of the string is the maximum length of a non-empty substring that contains only one unique character.

Given a string s, return the power of s.

Example 1:

Input: s = “leetcode”

Output: 2

Explanation: The substring “ee” is of length 2 with the character ‘e’ only.

Example 2:

Input: s = “abbcccddddeeeeedcba”

Output: 5

Explanation: The substring “eeeee” is of length 5 with the character ‘e’ only.

Example 3:

Input: s = “triplepillooooow”

Output: 5

Example 4:

Input: s = “hooraaaaaaaaaaay”

Output: 11

Example 5:

Input: s = “tourist”

Output: 1

Constraints:

1 <= s.length <= 500

s consists of only lowercase English letters.

LeetCode Çözümleri – How Many Numbers Are Smaller Than the Current Number

LeetCode içerisinde bulunan “How Many Numbers Are Smaller Than the Current Number” sorusunun açıklaması ve çözümü. Bu soruda sizi verilen bir tam sayı dizisi içerisinde, her bir sayı için bu sayıdan küçük kaç tane sayı olduğunu yine bir dizi olarak bulmanız isteniyor.

► LeetCode 1365. How Many Numbers Are Smaller Than the Current Number: https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/

► Problem açıklaması:

Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it. That is, for each nums[i] you have to count the number of valid j’s such that j != i and nums[j] lessThan nums[i].

Return the answer in an array.

Example 1:

Input: nums = [8,1,2,2,3]

Output: [4,0,1,1,3]

Explanation:

For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3).

For nums[1]=1 does not exist any smaller number than it.

For nums[2]=2 there exist one smaller number than it (1).

For nums[3]=2 there exist one smaller number than it (1).

For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).

Example 2:

Input: nums = [6,5,4,8]

Output: [2,1,0,3]

Example 3:

Input: nums = [7,7,7,7]

Output: [0,0,0,0]

Constraints:

2 <= nums.length <= 500

0 <= nums[i] <= 100

LeetCode Çözümleri – Find N Unique Integers Sum up to Zero

LeetCode içerisinde bulunan “Find N Unique Integers Sum up to Zero” sorusunun açıklaması ve çözümü. Bu soruda sizi verilen bir tam sayı büyüklüğünde, farklı tam sayılardan oluşan ve elemanları toplamı 0 olan herhangi bir dizi döndürmeniz isteniyor.

► LeetCode 1304. Find N Unique Integers Sum up to Zero: https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/

► Problem açıklaması:

Given an integer n, return any array containing n unique integers such that they add up to 0.

Example 1:

Input: n = 5

Output: [-7,-1,1,3,4]

Explanation: These arrays also are accepted [-5,-1,1,2,3] , [-3,-1,2,-2,4].

Example 2:

Input: n = 3

Output: [-1,0,1]

Example 3:

Input: n = 1

Output: [0]

Constraints:

1 <= n <= 1000

LeetCode Çözümleri – Check If N and Its Double Exist

LeetCode içerisinde bulunan “Check If N and Its Double Exist” sorusunun açıklaması ve çözümü. Bu soruda sizi verilen bir tam sayı dizisinde, herhangi bir elemanın iki katı veya yarısı olan başka bir eleman var mı diye soruluyor.

► LeetCode 1346. Check If N and Its Double Exist: https://leetcode.com/problems/check-if-n-and-its-double-exist

► Problem açıklaması:

Given an array arr of integers, check if there exists two integers N and M such that N is the double of M ( i.e. N = 2 * M).

More formally check if there exists two indices i and j such that :

i != j

0 <= i, j == arr.length

arr[i] == 2 * arr[j]

Example 1:

Input: arr = [10,2,5,3]

Output: true

Explanation: N = 10 is the double of M = 5,that is, 10 = 2 * 5.

Example 2:

Input: arr = [7,1,14,11]

Output: true

Explanation: N = 14 is the double of M = 7,that is, 14 = 2 * 7.

Example 3:

Input: arr = [3,1,7,11]

Output: false

Explanation: In this case does not exist N and M, such that N = 2 * M.

Constraints:

2 <= arr.length <= 500

-10^3 <= arr[i] <= 10^3

LeetCode Çözümleri – Replace Elements with Greatest Element on Right Side

LeetCode içerisinde bulunan “Replace Elements with Greatest Element on Right Side” sorusunun açıklaması ve çözümü. Bu soruda sizi verilen bir tam sayı dizisinde, her bir elemanı, sağındaki en büyük eleman ile replace etmenizi, son elemanı da -1’e atamanızı istiyor.

► LeetCode 1299. Replace Elements with Greatest Element on Right Side: https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/

► Problem açıklaması:

Given an array arr, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1.

After doing so, return the array.

Example 1:

Input: arr = [17,18,5,4,6,1]

Output: [18,6,6,6,1,-1]

Explanation:

– index 0 — the greatest element to the right of index 0 is index 1 (18).

– index 1 — the greatest element to the right of index 1 is index 4 (6).

– index 2 — the greatest element to the right of index 2 is index 4 (6).

– index 3 — the greatest element to the right of index 3 is index 4 (6).

– index 4 — the greatest element to the right of index 4 is index 5 (1).

– index 5 — there are no elements to the right of index 5, so we put -1.

Example 2:

Input: arr = [400]

Output: [-1]

Explanation: There are no elements to the right of index 0.

Constraints:

1 <= arr.length <= 10^4

1 <= arr[i] <= 10^5

Microsoft Mülakat Sorusu – Valid Parentheses

LeetCode içerisinde bulunan “Valid Parentheses” sorusunun açıklaması ve çözümü. Bu soruda sizi verilen bir string içerisindeki parantezlerin valid (açılan parantezin önce kapatılması ve bunların sırayla olması) olup olmadığını kontrol etmeniz isteniyor.

► LeetCode 20. Valid Parentheses: https://leetcode.com/problems/valid-parentheses/

► Problem açıklaması:

Given a string s containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ and ‘]’, determine if the input string is valid.

An input string is valid if:

Open brackets must be closed by the same type of brackets.

Open brackets must be closed in the correct order.

Example 1:

Input: s = “()”

Output: true

Example 2:

Input: s = “()[]{}”

Output: true

Example 3:

Input: s = “(]”

Output: false

Example 4:

Input: s = “([)]”

Output: false

Example 5:

Input: s = “{[]}”

Output: true

Constraints:

1 <= s.length <= 10^4

s consists of parentheses only ‘()[]{}’.

LeetCode Çözümleri – Find Numbers with Even Number of Digits

LeetCode içerisinde bulunan “Find Numbers with Even Number of Digits” sorusunun açıklaması ve çözümü. Bu soruda sizi verilen bir tam sayı dizisinde, kaç tane çift sayıda rakam içeren sayı olduğunu bulmanız isteniyor.

► LeetCode 1295. Find Numbers with Even Number of Digits: https://leetcode.com/problems/find-numbers-with-even-number-of-digits/

► Problem açıklaması:

Given an array nums of integers, return how many of them contain an even number of digits.

Example 1:

Input: nums = [12,345,2,6,7896]

Output: 2

Explanation:

12 contains 2 digits (even number of digits).

345 contains 3 digits (odd number of digits).

2 contains 1 digit (odd number of digits).

6 contains 1 digit (odd number of digits).

7896 contains 4 digits (even number of digits).

Therefore only 12 and 7896 contain an even number of digits.

Example 2:

Input: nums = [555,901,482,1771]

Output: 1

Explanation: Only 1771 contains an even number of digits.

Constraints: 1 <= nums.length <= 500

1 <= nums[i] <= 10^5

Hackerrank “30 Days of Code” Çözümleri – Day 29: Bitwise AND

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 29: Bitwise AND” sorusunun açıklaması ve çözümü. Bu soruda bitwise and (&) operatörünün nasıl çalıştığına bir örnek ile göz attık.

Hackerrank 30 Days of Code Çözümleri – Day 29: Bitwise AND: https://www.hackerrank.com/challenges/30-bitwise-and/problem

► Problem açıklaması:

Objective

Welcome to the last day! Today, we’re discussing bitwise operations. Check out the Tutorial tab for learning materials and an instructional video!

Task

Given set S = {1, 2, 3, .. N}. Find two integers, A and B (where A less B), from set S such that the value of A&B is the maximum possible and also less than a given integer, K. In this case, & represents the bitwise AND operator.

Function Description

Complete the bitwiseAnd function in the editor below.

bitwiseAnd has the following paramter(s):

– int N: the maximum integer to consider

– int K: the limit of the result, inclusive

Returns

– int: the maximum value of A&B within the limit.

Input Format

The first line contains an integer, T, the number of test cases. Each of the T subsequent lines defines a test case as 2 space-separated integers, N and K, respectively.

Sample Input

STDIN Function

—– ——–

3 T = 3

5 2 N = 5, K = 2

8 5 N = 8, K = 5

2 2 N = 8, K = 5

Sample Output

1

4

0