Posts

Java Data Structure Cheat Sheet

Average time complexity of data structures: Data structure Access (top) Access (key) Search Insertion Deletion Array O(1) O(1) O(N) O(N) O(N) Stack O(1) O(N) O(N) O(1) O(1) Queue O(1) O(N) O(N) O(1) O(1) Linked List O(1) O(N) O(N) O(1) O(1) Hash Table n/a O(1) O(1) O(1) O(1) Binary Search Tree O(1) O(log N) O(log N) O(log N) O(log N) Heap O(1) O(N) O(N) O(log N) O(log N) Each link is for a Java example.

Java Data Structure Cheat Sheet - Queue

Queue is an interface in Java. There are multiple implementation choices, but using LinkedList is common and performs as the usual queue as expected. https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html Main methods are: boolean add(E e) E element() E remove() and there exists the alternative ones as: boolean offer(E e) E peek() E poll() The first group throws an Exception whereas the second one returns "special value". They are all O(1). Example import java.util.LinkedList; import java.util.Queue; public class QueueExample {   public static void main(String[] args ) {     Queue<Integer> q = new LinkedList<Integer>();     q .add(1); // or q.offer(1)     q .add(2);     q .add(3);     System. out .println( q .element()); // 1. you can also use q.peek()     while (! q .isEmpty()) { // isEmpty() is from Collection       int n = q .remove(); // or q.poll()   ...

kind (multi-node k8s cluster): Quick Start on Mac

This post shows an example usage of kind  on Mac. what is kind? kind lets you create a k8s cluster with multiple nodes on docker. You need docker & kubectl to run it. install % docker -v Docker version 20.10.5, build 55c4c88 % kubectl version ... v1.19.7 ... % brew install kind % kind --version kind version 0.11.0 one-node cluster You can create a one-node cluster by default. The initial creation may take a few minutes longer to load the image. % kind create cluster % kubectl get node NAME                 STATUS     ROLES                  AGE   VERSION kind-control-plane   NotReady   control-plane,master   30s   v1.21.1 % kind delete cluster multi-node cluster Passing a config file (--config) lets you create a cluster with multiple nodes. The following example will create 3 controle-planes & 3 workers. % cat kind.yaml...

Java Data Structure Cheat Sheet - Stack

Stack exists as a class (not as an interface or abstract class) in Java. https://docs.oracle.com/javase/7/docs/api/java/util/Stack.html Here are the three main methods: E peek() E pop() E push(E item) They are all O(1). Example: import java.util.Stack; public class StackExample {   public static void main(String[] args ) {     Stack<Integer> s = new Stack<Integer>();     s .push(1); // add is also available as Vector     s .push(2);     s .push(3);     System. out .println( s .peek()); // 3     while (! s .empty()) { // isEmpty() is also available as Vector       int n = s .pop();       System. out .println( n ); // 3, 2, 1     }     // System.out.println(s.peek()); // java.util.EmptyStackException is called   } }

Assign Domain on Route53 to CloudFront

Image
In this post, I am going to assign a domain name (on Route53) to the website hosted on S3 thru CloudFront ( prev post ). user <-> Route53       -> CloudFront -> S3 Issue & Validate Certificate Console > Certificate Manager (ACM) > Request a certificate Add domain names > Domain name: *.YOUR_DOMAIN > Next Select validation method > DNS validation  > Next Add tags > Review Review > Confirm and request Validation > Create record in Route 53 > Continue The validation will take a few minutes. Add CNAME to CloudFront Console > CloudFront > [cloud-front-id] > Edit Alternate Domain Names: www.YOUR_DOMAIN SSL Certificate: Custom SSL Certificate: (select your certificate) > Yes, Edit Add A Record Console > Route53 > Hosted zone > YOUR_DOMAIN > Create record: Record name: www Recird type: A & AAAAA (Turn on Alias ) Route traffic to: Alias to CloudFront distribution , Select distribution > Create rec...

Host Website on S3 thru CloudFront

In this post, I am going to describe how to host a website on S3, allowing the access from CloudFront for faster content distribution. user -> CloudFront -> S3 Create Bucket on S3 % mkdir web && cd web % echo 'Hello, World!' > index.html % aws s3 mb s3://YOUR_BUCKET_NAME % aws s3 cp index.html s3://YOUR_BUCKET_NAME Confirm that you cannot access the bucket directly. % curl https://s3.amazonaws.com/YOUR_BUCKET_NAME/index.html AccessDenied Create Distribution on CloudFront Console > CloudFront > Create Distribution > Get Started > Origin Domain Name: YOUR_BUCKET_NAME Restrict Bucket Access: Yes Origin Access Identity: Create a New Identity Grant Read Permissions on Bucket: Yes, Update Bucket Policy Default Root Object: index.html > Create Distribution This will take a few minutes. Access % aws cloudfront list-distributions | jq -r '.DistributionList.Items[0].DomainName' xxxxxxxxxxxxx.cloudfront.net % curl xxxxxxxxxxxxx.cloudfront.net Hello,...

Google Kubernetes Engine (GKE) Quick Start on CLI

Google Cloud provides a nice quickstart guide for GKE: https://cloud.google.com/kubernetes-engine/docs/quickstart This post follows the steps on CLI. You need to install gcloud & kubectl as a pre-requisite. # enable GKE % gcloud services enable container.googleapis.com % gcloud services list | grep -i 'kube' container.googleapis.com          Kubernetes Engine API # config gcloud % gcloud config set project YOUR_PROJECT_ID % gcloud config set compute/zone us-west1-a % gcloud config set compute/region us-west1 # create cluster (few minutes) % gcloud container clusters create hello-cluster --num-nodes=1 % gcloud container clusters list NAME           LOCATION    MASTER_VERSION   MASTER_IP     MACHINE_TYPE  NODE_VERSION     NUM_NODES  STATUS hello-cluster  us-west1-a  1.18.17-gke.100  35.230.17.63  e2-medium     1.18.17-gke.100  1...