How much can you really get out of a 4$ VPS?

I am a big fan of cheap VPSs. I start many personal projects, bring up a VPS, play around and then wipe it. Never have to worry about accidentally running up bill in 100s or 1000s of dollars. I run this blog on $4 server. So this was interesting to read how far can a cheap VPS go.

February 6, 2023 · 1 min · Amer Khalid

How to backup Kubernetes' Config

The easiest solution seems to be following script by Stackoverflow user, Timothy Perez : <pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">#!/bin/bash # NAMESPACED EXPORTS for ns in $( kubectl get ns --no-headers | cut -d " " -f1 ) ; do kubectl --namespace = " ${ ns} " get -o = json bindings,cm,ep,ev,limits,pvc,po,podtemplates,rc,quota,secrets,sa,svc,controllerrevisions,ds,deploy,rs,sts,localsubjectaccessreviews,hpa,cj,jobs,leases,ev,ds,deploy,ing,netpol,rs,pods,netpol,pdb,roles,rolebindings | \ jq '.items[] | select(.type!="kubernetes.io/service-account-token") | del( .spec.clusterIP, .metadata.uid, .metadata.selfLink, .metadata.resourceVersion, .metadata.creationTimestamp, .metadata.generation, .status, .spec.template.spec.securityContext, .spec.template.spec.dnsPolicy, .spec.template.spec.terminationGracePeriodSeconds, .spec.template.spec.restartPolicy )' >> "./ ${ ns} .json" done # NON-NAMESPACED EXPORTS kubectl get -o = json cs,ns,no,pv,mutatingwebhookconfigurations,validatingwebhookconfigurations,crds,apiservices,tokenreviews,selfsubjectaccessreviews,selfsubjectrulesreviews,subjectaccessreviews,csr,psp,nodes,psp,clusterrolebindings,clusterroles,pc,sc,volumeattachments | \ jq '.items[] | select(.type!="kubernetes.io/service-account-token") | del( .spec.clusterIP, .metadata.uid, .metadata.selfLink, .metadata.resourceVersion, .metadata.creationTimestamp, .metadata.generation, .status, .spec.template.spec.securityContext, .spec.template.spec.dnsPolicy, .spec.template.spec.terminationGracePeriodSeconds, .spec.template.spec.restartPolicy )' >> "./cluster_non-namespaced_export.json" Script copied here under CC BY-SA 4.0 license ...

January 20, 2022 · 1 min · Amer Khalid

LeetCode 605. Can Place Flowers

/** * @param {number[]} flowerbed * @param {number} n * @return {boolean} */ var canPlaceFlowers = function (flowerbed , n ) { for ( let i = 0 ; i < flowerbed .length ; i ++ ) { if (flowerbed [i ] == 0 && (i == 0 || flowerbed [i - 1 ] == 0 ) && (i == flowerbed .length - 1 || flowerbed [i + 1 ] == 0 )) { n -- flowerbed [i ] = 1 if (n < 1 ) return true } } return n <= 0 };

January 19, 2022 · 1 min · Amer Khalid

LeetCode 1268. Search Suggestions System

/** * @param {string[]} products * @param {string} searchWord * @return {string[][]} */ var suggestedProducts = function (products , searchWord ) { products .sort ((a , b ) => { if (a > b ) return 1 if (a < b ) return - 1 return 0 }) const r = [] for ( let i = 1 ; i <= searchWord .length ; i ++ ) { const arr = products .filter ((s ) => s .startsWith (searchWord .substring ( 0 , i ))).slice ( 0 , 3 ) //console.log(searchWord.substring(0, i)) r .push (arr ) } return r };

January 14, 2022 · 1 min · Amer Khalid

LeetCode 253. Meeting Rooms II

Lack of built-in Priority Queues in JavaScript makes it a bit hard to do problems like this. I end looking at the solution: /** * @param {number[][]} intervals * @return {number} */ var minMeetingRooms = function (intervals ) { if (intervals .length < 1 ) return 0 const startTimes = [] const endTimes = [] for ( let i = 0 ; i < intervals .length ; i ++ ) { startTimes [i ] = intervals [i ][ 0 ] endTimes [i ] = intervals [i ][ 1 ] } startTimes .sort ((a , b ) => a - b ) endTimes .sort ((a , b ) => a - b ) let startPtr = 0 let endPtr = 0 let rooms = 0 for ( let i = 0 ; i < intervals .length ; i ++ ) { if (startTimes [i ] >= endTimes [endPtr ]) { endPtr ++ } else { rooms ++ } } return rooms };

January 12, 2022 · 1 min · Amer Khalid

LeetCode 937. Reorder Data in Log Files

/** * @param {string[]} logs * @return {string[]} */ var reorderLogFiles = function(logs) { const sorted = [] const numLogs = [] const charLogs = [] for (let i = 0; i<logs.length; i++) { const log = logs[i] const tokenized = log.split(" ") if (Number.isInteger(parseInt(tokenized[1]))) { numLogs.push(log) } else { charLogs.push(log) } } charLogs.sort(function (a, b) { const stra = a.substr(a.indexOf(' ') + 1) const strb = b.substr(b.indexOf(' ') + 1) if (stra > strb) { return 1 } if (stra < strb) { return -1 } const ida = a.substr(0, a.indexOf(' ')) const idb = b.substr(0, b.indexOf(' ')) //console.log({ida}) if (ida > idb) { return 1 } if (ida < idb) { return -1 } return 0 }) return [...charLogs, ...numLogs] };

January 11, 2022 · 1 min · Amer Khalid

LeetCode 49. Group Anagrams

/** * @param {string[]} strs * @return {string[][]} */ var groupAnagrams = function(strs) { const map = [] for (let i = 0; i<strs.length; i++) { const str = strs[i].split('').sort().join('') if (!map[str]) map[str] = [] map[str].push(strs[i]) } const ans = [] for (let k in map) { ans.push(map[k]) } return ans };

January 6, 2022 · 1 min · Amer Khalid

LeetCode 71. Simplify Path

/** * @param {string} path * @return {string} */ var simplifyPath = function(path) { const simple = []; const splitPath = path.split("/").filter(x => x.length>0 && x !== '.') for (let i = 0; i<splitPath.length; i++) { if (splitPath[i] === '..') { simple.pop() } else { simple.push(splitPath[i]) } } return `/${simple.join('/')}` };

January 5, 2022 · 1 min · Amer Khalid

An Algorithm for Passing Programming Interviews | Hacker News

An Algorithm for Passing Programming Interviews | Hacker News — Read on news.ycombinator.com/item

January 3, 2022 · 1 min · Amer Khalid

LeetCode 50. Pow(x, n)

I got stuck on this problem, even after looking at answers, I could not follow the algorithm. Here is my solution: /** * @param {number} x * @param {number} n * @return {number} */ var myPow = function (x , n ) { if (x == 0 ) return 0 if (n == 0 ) return 1 if (n < 0 ) { x = 1 / x n = - 1 * n } const half = myPow (x , Math.floor (n / 2 )) let ans = half * half if (n % 2 == 1 ) ans = x * ans return ans };

December 31, 2021 · 1 min · Amer Khalid