Category Archives: Algorithms

Atlassian Hackerrank Interview

1) Implement a method ‘find’ that will find the starting index (zero based) where the second list occurs as a sub-list in the first list. It should return -1 if the sub-list cannot be found. Arguments are always given, not … Continue reading

Posted in Algorithms | Leave a comment

Combinations of a String

Implement a function that prints all possible combinations of a string. A string “12” is the same as string “21” function combine($instr, $outstr, $index) { for ($i = $index; $i < strlen($instr); $i++) { $outstr = $outstr . $instr[$i]; echo … Continue reading

Posted in Algorithms | Leave a comment

Given Array of positive negative numbers find 2,3 of them that sum to zero

Given an array of numbers with positive and negative, find pairs that sum to zero   a) sort the numbers b) start 2 pointers from from begin and end c) if sum is > or < = then move appropriate … Continue reading

Posted in Algorithms | Leave a comment

Find if an integer is a prime number

The algorithm can be improved further by observing that all primes are of the form 6k ± 1, with the exception of 2 and 3. This is because all integers can be expressed as (6k + i) for some integer … Continue reading

Posted in Algorithms | Leave a comment

Trees: Find count of unival or single valued subtrees

http://www.geeksforgeeks.org/find-count-of-singly-subtrees/

Posted in Algorithms | Leave a comment

Towers of Hanoi

https://www.youtube.com/watch?v=q6RicK1FCUs Karumanchi, pg 37.

Posted in Algorithms | Leave a comment

Sort an array of 0s, 1s and 2s

Given an array A[] consisting 0s, 1s and 2s, write a function that sorts A[]. The functions should put all 0s first, then all 1s and all 2s in last. Example Input = {0, 1, 1, 0, 1, 2, 1, … Continue reading

Posted in Algorithms | Leave a comment

Longest Common Subsequence

https://www.youtube.com/watch?v=NnD96abizww if (input1[i] == input2[j]) { M[i][j] = M[i-1][j-1] + 1 } else { M[i][j] = max(M[i-1,j], M[i,j-1]) }

Posted in Algorithms | Leave a comment

Sort Algorithms

Stable Sort: A sorting algorithm is said to be stable if two objects with equal keys appear in the same order in sorted output as they appear in the input unsorted array. Some sorting algorithms are stable by nature like … Continue reading

Posted in Algorithms | Leave a comment

Find kth largest element in an unsorted array

Method 1 (Simple Solution) A Simple Solution is to sort the given array using a O(nlogn) sorting algorithm like Merge Sort, Heap Sort, etc and return the element at index k-1 in the sorted array. Time Complexity of this solution … Continue reading

Posted in Algorithms | Leave a comment