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 / baseConversion.scala
Created August 9, 2020 21:41
Base conversion - like base 10 to 62 or base 10 to 32
/**
* convert base 10 to given new base
*/
def baseConversion(base: Int, toBeConvert: Int): List[Int] = {
def compute(quotient: Int, acc: List[Int]): List[Int] = {
if(quotient<=0) {
acc.reverse
} else {
val q = quotient / base
val r = quotient % base
@asethia
asethia / QueueViaStacks.scala
Created July 30, 2018 19:56
Implement a MyQueue class which implements a queue using two stacks.
/**
* Implement a MyQueue class which implements a queue using two stacks.
* Time Complexity
* Enqueue O(1), DeQueue O(n) if dequeue called after each enqueue
* Space complexity O(n)
* Created by Arun Sethia on 7/30/18.
*/
case class QueueViaStacks(stack: List[Int], queue: List[Int] = Nil) {
def enQueue(elem: Int) = QueueViaStacks(elem :: stack, queue)
@asethia
asethia / MinStack.scala
Created July 30, 2018 15:58
Design a stack which, in addition to push and pop, has a function min which returns the minimum element.
/**
* Design a stack which, in addition to push and pop, has a function min which returns the minimum element.
* Push, pop and min should all operate in 0(1) time.
* Created by Arun Sethia on 7/29/18.
*/
case class MinStack(data: List[Int], min: Int) {
/**
*
* @param elem
* @return
@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)
*
@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 / 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 / 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 / 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 / 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 / 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 {