Categories
Typescript

Easy logging rxjs

import {Observable} from ‘rxjs’; import {tap} from ‘rxjs/operators’; export enum RxJsLoggingLevel { TRACE, DEBUG, INFO, WARN, ERROR } let rxjsLoggingLevel = RxJsLoggingLevel.TRACE; export function setRxJsLoggingLevel(level: RxJsLoggingLevel) { rxjsLoggingLevel = level; } function a(level: number) { return (message: string) => (source: Observable) => source .pipe( tap(val => { if (level >= rxjsLoggingLevel) { console.log(message + ‘: […]

Categories
iOS

The pitfall in using weak referenced delegations for observer pattern

As Android and Java expert, recently I have came across a very nice iOS implementation for the observer pattern that exploits swift’s ARC to automatically “remove” the observer, this of course cannot be implemented in a JVM GC as unlike ARC, GC does not guarantee clearing weak references right on time. It was so good and simple […]

Categories
Interview Questions

Lowest Common Ancestor of a Binary Tree

class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (root==null || root==p || root==q) return root; TreeNode left = lowestCommonAncestor(root.left,p,q); TreeNode right = lowestCommonAncestor(root.right,p,q); if ((left==p && right==q) || (left==q && right==p)) return root; if (left!=null) return left; return right; } } Since for every node in a binary tree it […]

Categories
Interview Questions

Making A Large Island

class Solution { public int largestIsland(int[][] grid) { if (grid.length==0) return 0; Map map = mapIslands(grid); int max = map.values().stream().reduce(Integer::max).get(); for (int i = 0; i

Categories
Interview Questions

Best Time to Buy and Sell Stock with K transaction

The original question can be found here.  Lets start from an elegant the solution : class Solution { public int maxProfit(int k,int[] prices) { if (prices.length

Categories
Android

Debouncing a function

I will start with a very common use case – say we have events that occurs a lot and in random, sometimes small intervals and we want to have them written to disk as soon as possible. We could simply write them to disk immediately after every event, but if we look back at rule […]

Categories
Python

cluster1d a single dimensional clustering algorithm

I was looking for a single dimension clustering algorithm back in the day and i didnt really find one implemented “out of the box” so I took the challenge and implemented one myself, I called the resulting algorithm “cluster1d” and you can see it in full here. Its runtime complexity is O(nlogn) and space complexity […]

Categories
Android

Ten Commandments For Developing a SDK on Android

1. You shall not crash! It is much harder to gain trust, than it is to lose it, when your SDK code crashes a customer’s app you can lose them right at this moment and it may be irreversible. You must consider all edge cases and deal with them, you must catch all your exceptions […]

Categories
Python

Convert Neural Network Inputs to Time Series

A Typical and simple neural network inputs would consist of 2 dimensions : Sample dimension Feature dimension To clarify their meaning, The length of Sample dimension of the given input indicates how many data items are we going to input, while the length of Feature dimension indicates how many data features does each data item […]

Categories
Python

Visualizing Training of Neural Networks

When I started working on neural networks deep learning projects, I was always curious about whats going on under the hood, observing TensorFlow’s output was not enough to realize how good my model really trains and it doesn’t provide enough clarity of how the model progresses towards the goal I want it to learn. After […]