Skip to content

Instantly share code, notes, and snippets.

View asethia's full-sized avatar

Arun Sethia asethia

  • United States
View GitHub Profile
@asethia
asethia / StringRotation.scala
Last active July 8, 2018 14:20
Assume you have a method isSubstring which checks if one word is a substring of another.
import org.scalatest.{Matchers, WordSpec}
import scala.annotation.tailrec
/**
* Assume you have a method isSubstring which checks if one word is a substring of another.
* Given two strings, 51 and 52, write code to check if 52 is a rotation of 51 using only
* one call to isSubstring (e.g.,"waterbottle"is a rotation of"erbottlewat").
* Time Complexity: O(n)
* Created by Arun Sethia on 7/7/18.
@asethia
asethia / RotateMatrix.scala
Last active July 15, 2018 19:02
Given an NxN matrix, write a method to rotate the by 90 degrees.
import org.scalatest.{Matchers, FunSpec, WordSpec}
import scala.util.Try
/**
* Given an image represented by an NxN matrix,
* write a method to rotate the by 90 degrees.
* do this in place.
* Time Complexity O(n*n)
* Created by Arun Sethia on 7/8/18.
@asethia
asethia / LinkListRemoveDuplicateUsingHahsMap.scala
Created July 17, 2018 03:00
LinkList remove duplicates using HashMap
package linkedlist
import scala.annotation.tailrec
import scala.collection.immutable.HashMap
/**
* LinkList remove duplicates using HashMap
* Time Complexity O(n)
* Space Complexity O(n) for hashmap if all are duplicates
@asethia
asethia / LinklistKthLastSpec.scala
Created July 18, 2018 02:42
Implement an algorithm to find the kth to last element of a singly linked list.
import org.scalatest.{Matchers, WordSpec}
/**
* Implement an algorithm to find the kth to last element of a singly linked list.
* time complexity O(n)
* space complexity O(1)
* Created by Arun Sethia on 7/17/18.
*/
trait Linklist {
@asethia
asethia / LinklistMiddleSpec.scala
Last active July 20, 2018 15:49
Implement an algorithm to find the middle element of a singly linked list.
import org.scalatest.{Matchers, WordSpec}
/**
* Implement an algorithm to find the middle element of a singly linked list.
* time complexity O(n)
* space complexity O(1)
* Created by Arun Sethia on 7/17/18.
*/
trait Linklist {
@asethia
asethia / LinkRemoveMiddleSpec.scala
Last active July 21, 2018 16:34
Implement an algorithm to remove the middle element of a singly linked list.
import org.scalatest.{Matchers, WordSpec}
/**
* Implement an algorithm to remove the middle element of a singly linked list.
* and return new linklist
* time complexity O(n)
* space complexity O(n/2)
* Created by Arun Sethia on 7/17/18.
*/
trait Linklist {
@asethia
asethia / LinkSumListSpec.scala
Last active July 23, 2018 03:46
SumList - sum of two link list result as link list
import org.scalatest.{Matchers, WordSpec}
import scala.annotation.tailrec
/**
* You have two numbers represented by a linked list, where each node contains a single digit.
* The digits are stored in reverse order, such that the 1's digit is at the head of the list.
* Write a function that adds the two numbers and returns the sum as a linked list.
*
* Assumptions: both list has equal number of nodes
@asethia
asethia / LinklistLoopDetect.scala
Created July 24, 2018 14:20
Given a circular linked list, implement an algorithm that returns if loop exist
import org.scalatest.WordSpec
import scala.annotation.tailrec
/**
* Given a circular linked list, implement an algorithm that returns if loop exist
*
* Time Complexity O(n) and Space Complexity zero
*
* Created by Arun Sethia on 7/23/18.
@asethia
asethia / LinklistLoopNode.scala
Created July 24, 2018 14:34
Given a circular linked list, implement an algorithm that returns the node at the beginning of the loop.
import scala.annotation.tailrec
/**
* Given a circular linked list, implement an algorithm that returns
* the node at the beginning of the loop.
*
* Time Complexity O(n) and Space Complexity O(n)
*
* Created by Arun Sethia on 7/23/18.
*/
@asethia
asethia / LinklistIntersection.scala
Created July 24, 2018 20:21
Given a circular linked list, implement an algorithm that returns the node at the beginning of the loop.
package linklist4
import scala.annotation.tailrec
/**
* Given a circular linked list, implement an algorithm that returns
* the node at the beginning of the loop.
*
* Time Complexity O(n+m) and Space Complexity O(n)
*