This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
NewerOlder