Category: iOS

  • Sparse Arrays – Hackerrank medium problem in JS solved using HashMap

    https://www.hackerrank.com/challenges/sparse-arrays/problem The idea is to iterate over input data and put it into HashMap with keys being available strings and values being the number of times this string is present in the input. Time complexity is O(n) and space complexity O(n)

  • Moving boxes to one spot – Leetcode coding problem (Medium)

    Another fun problem that was pretty easy solve is moving boxes to one spot: https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/ It can be solved by iterating over an array and summing difference in distance between elements. Solution in Javascript:

  • Same Tree with Swift

    Problem: Given two binary trees, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical and the nodes have the same value. Solution Using Breadth-First Search traverse trees and compare nodes during traversal. /** * Definition for a binary tree node.…

  • Clone connected graph in Swift

    Given a reference of a node in a connected undirected graph. Return a deep copy (clone) of the graph. Solution: Create a queue, process node and it’s neighbors, create copies of nodes processed and create a reference in a dictionary between original and new nodes. Traverse dictionary to reestablish a relationship for new neighbors for…

  • Count the number of ways to traverse a Matrix in Swift

    Question: Count the number of ways to traverse a from the top left corner to the bottom, right corner. Limitation: we can only move down or right. Few solutions: first one brute force and second dynamic programming and measuring their time execution below. To compute 10 x 10 matrix with recursive brute force took 1.4…

  • Minimum absolute difference in an array with Go

    Problem url: https://www.hackerrank.com/challenges/minimum-absolute-difference-in-an-array/problem Solution: Results are generated in O(n)*log(n) since we are sorting the array first. We can transfer sorted array and compare each number with the next find to find the smallest difference. func minimumAbsoluteDifference(arr []int32) int32 { var f foo f = arr temp := Abs(f[0] – f[1]) sort.Sort(f) fmt.Println(f) for i:=0; i…

  • Migratory Birds problem from Hackerrank with Go

    Problem url: https://www.hackerrank.com/challenges/migratory-birds/problem Solution: Results are generated in O(1) while traversing an array. We need to keep track of the largest value and keep a record of the smallest key. func migratoryBirds(arr []int32) int32 { //key value m := make(map[int32]int32) largest := int32(0) largestKey := int32(0) for _, key := range arr { c :=…

  • Min Max Sum from hackerrank on C++

    Min Max Sum from hackerrank on C++

    Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers. Full question is over here:hackerrank Working solution #include <bits/stdc++.h> using namespace std; void miniMaxSum(vector <int> arr)…

  • Merge Sort in Objective-C

    An implementation of the merge sort. Coded according to a Top-Down C example from Wikipedia. https://en.wikipedia.org/wiki/Merge_sort // Array A[] has the items to sort; array B[] is a work array. – (void)topDownMergeSort:(NSMutableArray*)A array:(NSMutableArray*) B int:(int) n{ [self copyArray:A begin:0 end:n array:B]; // duplicate array A[] into B[] [self topDownSplitMerge:B begin:0 end:n array:A];// sort data from…

  • Clone Undirected Graph with Javascript

    A question that I got asked on the interview at Facebook on 2016 that I wasn’t able to finish coding within interview time frame. Finally got a chance to put it into code. I used a LeetCode as a JS platform to test a code: https://leetcode.com/problems/clone-graph/description/ /** * Definition for undirected graph. * function UndirectedGraphNode(label)…