Skip to content

Instantly share code, notes, and snippets.

View anoopelias's full-sized avatar

Anoop Elias anoopelias

View GitHub Profile
@anoopelias
anoopelias / AppleStock.java
Created October 17, 2013 07:22
Given the price of stock for every minute past midnight, find the time at which you would have bought and sold for maximum profit.
public class AppleStock {
public static void main(String[] args) {
int[] stockPricesYesterday = { 500, 501, 503, 495, 503 };
int buy = -1;
int sell = -1;
int lowest = -1;
for (int i = 0; i < stockPricesYesterday.length; i++) {
@anoopelias
anoopelias / Recursion.md
Last active December 27, 2015 13:29
Recursion

There are two forms of recursion,

  • Tail Recursion : The return value is calculated as a combination of the value of current subroutine and the return value of the next call. Example,

     int factorial(int a) {
         if(a==0)
             return 1;
         else
             return a * factorial( a-1 );
    

}

@anoopelias
anoopelias / Tree.java
Last active December 28, 2015 01:29
Algorithms || - Job Interview Question
import java.util.ArrayList;
import java.util.List;
/**
*
* Diameter and center of a tree.
*
* Given a connected graph with no cycles,
*
* Diameter: design a linear-time algorithm to find the longest simple path in
@anoopelias
anoopelias / EulerPath_small.txt
Last active December 28, 2015 02:49
Algorithms II - Job Interview Question
10
14
9 3
2 5
6 5
9 8
0 3
4 7
2 1
0 6
use std::cmp::max;
fn largest2(arr: [i32; 5]) -> i32 {
arr.iter().fold(0, |num, &x| {
max(num, x)
})
}
fn largest3(arr: [i32; 5]) -> i32 {
arr.iter().fold(0, max) // Do Not compile!

Keybase proof

I hereby claim:

  • I am anoopelias on github.
  • I am anoopelias (https://keybase.io/anoopelias) on keybase.
  • I have a public key ASBFp65xGHBRZOaP1q61K6Hvb_KSUY9TZzbbnSAh4ythugo

To claim this, I am signing this object:

@anoopelias
anoopelias / Manifest.md
Last active January 20, 2019 08:24
Using Manifest to get around with type erasure in scala

Typically, you can't use a 'new' operator on a generic type. This is because of type erasure.

scala> def create[T] = new T
<console>:7: error: class type required but T found
   def create[T] = new T
                       ^

Scala gives a way of getting around this problem, with the Manifest class.

scala> def create[T](implicit m:Manifest[T]) = m.erasure.newInstance

@anoopelias
anoopelias / Readme.md
Last active August 12, 2019 04:37
Firefox WebExtensions API `search.get()` throws an error when invoked from Ubuntu distribution

Prerequisites

Firefox 68.0.1 (64bit) installed from Ubuntu distribution,

Steps to reproduce

  1. Copy the files manifest.json and background.js in to a folder
  2. Run $ web-ext run
  3. In address bar type about:debugging. Temporary extensions 'Test Extension' will be loaded.
  4. Click 'Debug'
@anoopelias
anoopelias / TypeClasses.md
Last active December 7, 2021 23:42
What is the fuzz about Type Classes? #scala

Assume you have a couple of classes,

case class Person(name: String, age: Int)
case class Employee(person: Person, salary: Double)

Lets say for some reason,

  • You don't want to change this source, but
  • You should be able to add features save(person) and save(employee), and
  • You need clearly separate implementations for both the types,
@anoopelias
anoopelias / goroutines.go
Last active March 4, 2023 10:48
Redis set/get implementation in Go using goroutines
package main
import (
"bufio"
"fmt"
"net"
"strconv"
"strings"
)