2015 Kata Kana Media. All Rights Reserved. This work may be reproduced andredistributed, in whole or in part, without alteration and without priorwritten permission, solely by educational institutions for nonprofitadministrative or educational purposes provided all copies contain thefollowing statement: " 2015 Kata Kana Media. This work is reproduced anddistributed with the permission of the Kata Kana Media.
No other use ispermitted without the express prior written permission of the Kata Kana Media.For permission contact: .
Chapter1: Sample Katas
Partition Array
Problem
Given an unsorted array A of length
L, partition thearray around the first item, A[0].
Variation 1: now partition the array around the lastitem, A[
L - 1].
Illustration
For example, given an original array A:[,3,7,1,5,2,6],we want to partition the array A around its first item A[0] = 4. The resultingarray A will be partitioned such that all items with values less than thepivot value, in this case 4, will be put on its left and all other itemsgreater than or equal to the pivot value, will be put on its right: A:[3,2,3,,5,7,6].
Solution
To partition the array around the pivot value, in this casethe first item in the array, we will use two indices, say
i and
jand they start from the left-most and right-most of the array respectively.
Increment i as long as it is still less than jand the current i-th value A[i] is smaller than the pivotvalue. Do the opposite, decrement j as long as the j-thvalue is larger than the pivot value. If i and j do not meet andcross each other, swap A[i] and A[j]. Otherwise, break out of the outerloop, swap the pivot value, A[0] and A[j]. The last swapwill place the pivot value at its final desired position. Congratulation! you have just learned one of the mostimportant pattern in Algorithm.
This technique is the used by QuickSortalgorithm.
void Swap( int32_t * values, const size_t i, const size_t j) { int32_t tmp = values[i]; values[i] = values[j]; values[j] = tmp; } int32_t Partition( int32_t * values, const int32_t s, const int32_t e) { // ASSERT(values != nullptr && s < e); const auto pivot = s; const auto target = values[pivot]; int32_t i = s; int32_t j = e + 1; while (i < j) { do ++i; while (i < j && values[i]< target); do --j; while (values[j] > target); if (i > j) { break ; } Swap(values, i, j); } Swap(values, j, s); return j; }
Chapter 2: List of Problems
Reverse (Linked List)
Given a singly Linked List,reverse the next pointer to point to the previous node and return the new headthat points to the tail of the original Linked List.
Variation 1: ReverseDoubly Linked List
DetectCycle (Linked List)
Given a Linked List, check if ithas a cycle.
Variation 1: Return thenode where the cycle begins (i.e. Node 2 in the first example below)
IsPalindrome (String)
Test if a given string apalindrome. racecar is anotherpalindromic string. abcde is not a palindrome.
Variation 1: Given a stringwith white spaces and alpha-numerics characters, test that a string a1b c b2ais a palindrome.
Variation 1: Given a stringwith white spaces and alpha-numerics characters, test that a string a1b c b2ais a palindrome.
Note that any numeric and white spaces characters does notcount into consideration and can be ignored. Another valid palindrome examplesa bcba, 111ab1a. Variation 2: Given aninteger, test that 121 is a palindrome, 123 is NOT, 1023201 is a palindrome.
Reverse(String)
Reverse a string. For exampleReverse(helloworld) returns dlrowolleh.
Variation 1: reverse wordsin string.
Reverse(hello world) returns world hello. Variation 2: reverse digitsin integer. Reverse(12345) returns 54321. Variation 3: reverse binarybits of 32 bit integer. Reverse(0x00000005), returns 0x5000000. 3^j . 5^k
Hamming numbers, also knownas
ugly numbers and also
5-smooth numbers (numberswhose prime divisors are less or equal to 5), are numbers of the form
![The task is to return the first k-thhamming number with Ok space and Ok - photo 1](/uploads/posts/book/82277/images/00001.jpeg)
. 5^k
Hamming numbers, also knownas
ugly numbers and also
5-smooth numbers (numberswhose prime divisors are less or equal to 5), are numbers of the form
![The task is to return the first k-thhamming number with Ok space and Ok - photo 1](/uploads/posts/book/82277/images/00001.jpeg)
.
The task is to return the first k-thhamming number with O(k) space and O(k) runtime complexity. Variation 1: Show the firsttwenty Hamming numbers. Variation 2: Show the1691st Hamming number (the last one below 2). Variation 3: Print allprime factors of a given number.
Sqrt(x)
Implement a square root function -sqrt(x).
Variation 2: implementCubeRoot(x).
Pow(x,n)
Implement pow(x, n) efficiently.What is the runtime complexity of your solution?
Variation 1: implementsum(x, y) without using addition + operator. (HINTS: use only XOR and ANDbitwise operations).
Sum Big Integers (String)
Sum 2 large positive integersrepresented as strings.