LeetCode 1570. Dot Product of Two Sparse Vectors

My solution in JavaScript: /** * @param {number[]} nums * @return {SparseVector} */ var SparseVector = function (nums ) { const x = new Map () for ( let i = 0 ; i < nums .length ; i ++ ) { if (nums [i ] != 0 ) { x .set (i , nums [i ]) } } this.map = x this.length = nums .length return this }; // Return the dotProduct of two sparse vectors /** * @param {SparseVector} vec * @return {number} */ SparseVector .prototype .dotProduct = function (vec ) { let ans = 0 ; for ( let i = 0 ; i < vec .length ; i ++ ) { if (vec .map .has (i ) && this.map .has (i )) { ans += vec .map .get (i ) * this.map .get (i ) } } return ans }; // Your SparseVector object will be instantiated and called as such: // let v1 = new SparseVector(nums1); // let v2 = new SparseVector(nums2); // let ans = v1.dotProduct(v2);

December 30, 2021 · 1 min · Amer Khalid

Kubernetes not passing full path to Nodejs

I spent hours troubleshooting why url path was getting stripped from requests in our Nodejs/Expressjs based app. The reason was this line in Kubernetes’ Ingress: nginx.ingress.kubernetes.io/rewrite-target : / Removing it fixed the issue. Check Kubernetes documentation for more details.

December 29, 2021 · 1 min · Amer Khalid

Ask HN: Software Engineer hitting 40: what's next?

Recent thread on Hacker News, https://news.ycombinator.com/item?id=29360119, was very interesting for me especially since I turned 40 this year. Programming is something I enjoy and pretty good at it. I’m still not at the top of salary range for programmers but this is a tough question. I can get into FAANG but there is still a ceiling of how much one can earn as a software engineer. This comment says it best: ...

December 1, 2021 · 1 min · Amer Khalid

Leetcode 92: Reverse Linked List II

Here is my leetcode solution in JavaScript: /** * Definition for singly-linked list. * function ListNode(val, next) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } */ /** * @param {ListNode} head * @param {number} left * @param {number} right * @return {ListNode} */ var reverseBetween = function(head, left, right) { let currPos = 1; let currNode = head; let startNode = head; let endNode = null; while(currPos < left) { startNode = currNode; currNode = currNode.next; currPos++; } let newList = null; let tail = currNode; while (currPos <= right) { const next = currNode.next; currNode.next = newList; newList = currNode; currNode = next; currPos++; } startNode.next = newList; tail.next = currNode; if (left > 1) { return head; } return newList; }; Link to problem. ...

November 4, 2021 · 1 min · Amer Khalid

Wealth Lab Pro Earning Play Screener

This is a quick script that I use to find options to buy or sell. using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators; using Community.Components; namespace WealthLab.Strategies { public class MyStrategy : WealthScript { protected override void Execute() { DataSeries maFast = EMAModern.Series(Close, 50 ); DataSeries maSlow = EMAModern.Series(Close, 200 ); DataSeries maFast_1 = EMAModern.Series(Close, 10 ); DataSeries maSlow_2 = EMAModern.Series(Close, 50 ); DataSeries ma = EMAModern.Series(Close, 10 ); DataSeries maFast_3 = EMAModern.Series(Close, 10 ); DataSeries maSlow_4 = EMAModern.Series(Close, 50 ); PlotSeries(PricePane,EMAModern.Series(Close, 50 ),Color.Red,LineStyle.Solid, 2 ); PlotSeries(PricePane,EMAModern.Series(Close, 200 ),Color.Green,LineStyle.Solid, 2 ); PlotSeries(PricePane,EMAModern.Series(Close, 10 ),Color.Blue,LineStyle.Solid, 2 ); PlotSeries(PricePane,EMAModern.Series(Close, 50 ),Color.Red,LineStyle.Solid, 2 ); PlotSeries(PricePane,EMAModern.Series(Close, 10 ),Color.Blue,LineStyle.Solid, 2 ); //for(int bar = GetTradingLoopStartBar(201); bar < Bars.Count; bar++) int bar = Bars.Count - 1 ; { if (IsLastPositionActive) { Position p = LastPosition; if (p.EntrySignal.Contains("Group1|" )) { if (CrossUnder(bar, maFast_3, maSlow_4)) { SellAtMarket(bar + 1 , p, "Group1" ); } } } else { if (maFast[bar] > maSlow[bar]) { if (maFast_1[bar] > maSlow_2[bar]) { if (Close[bar] < ma[bar]) { if (EarningsDate.InWindow( this , bar, "earnings per share" , 7 , 0 )) { BuyAtMarket(bar + 1 , "Group1|" ); } } } } } } } } }

September 29, 2021 · 1 min · Amer Khalid

Oculus Quest 2 Review

I was debating between PS5 and Quest 2 (Affiliate link), and yes it is like comparing apples and oranges, but in the end Quest 2 won. I will likely buy PS5 at some point though. But before I buy PS5, I need a new TV with 4K and HDR. It seems pointless to play PS5 on 1080p TV from early 2010s. Quest 2 is not my first experience with Virtual Reality. I tried PSVR about 2-3 years ago but the mess of wires really turned me off with the idea of VR. When I found out that Quest 2 is completely wireless, I got interested in it again. I was not very excited about needing Facebook account to use it though. ...

September 27, 2021 · 3 min · Amer Khalid

LeetCode 206. Reverse Linked List

Here is a quick solution to Reverse Linked List problem. /** * Definition for singly-linked list. * function ListNode(val, next) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } */ /** * @param {ListNode} head * @return {ListNode} */ var reverseList = function(head) { let curr = head; let prev = null; while (curr) { let next = curr.next; curr.next = prev; prev = curr; curr = next; } return prev; };

June 8, 2021 · 1 min · Amer Khalid

Importing plotly failed. Interactive plots will not work.

Fix: pip3 install --upgrade plotly

June 2, 2021 · 1 min · Amer Khalid

LeetCode 560. Subarray Sum Equals K

Brute Force Solution to Subarray Sum Equals K problem from LeetCode: /** * @param {number[]} nums * @param {number} k * @return {number} */ var subarraySum = function(nums, k) { let ans = 0; for (let i=0; i<nums.length; i++) { let currSum = nums[i]; if (currSum === k) ans++; for (let j=i+1; j<nums.length; j++) { currSum += nums[j]; if (currSum === k) ans++; } } return ans; }; Optimized solution after cheating a bit: /** * @param {number[]} nums * @param {number} k * @return {number} */ var subarraySum = function(nums, k) { let ans = 0; const m = new Map(); let sum=0; m.set(sum, 1); for (let i=0; i<nums.length; i++) { sum += nums[i]; if (m.has(sum-k)) { ans += m.get(sum-k); } if (!m.has(sum)) { m.set(sum, 1); } else { m.set(sum, m.get(sum) + 1); } } return ans; }; Runtime: 96 ms, faster than 94.76% of JavaScript online submissions for Subarray Sum Equals K. ...

May 21, 2021 · 1 min · Amer Khalid

LeetCode 2. Add Two Numbers

Here is my solution to Add Two Numbers problem in PHP: /** * Definition for a singly-linked list. * class ListNode { * public $val = 0; * public $next = null; * function __construct($val = 0, $next = null) { * $this->val = $val; * $this->next = $next; * } * } */ class Solution { /** * @param ListNode $l1 * @param ListNode $l2 * @return ListNode */ function addTwoNumbers($l1, $l2) { $ans = new ListNode(0, null); $p = $l1; $q = $l2; $current = $ans; $carry=0; while ($p != null || $q != null) { $x = isset($p) ? $p->val : 0; $y = isset($q) ? $q->val : 0; $sum = $carry + $x + $y; $carry = (int) ($sum/10); $current->next = new ListNode($sum%10); $current = $current->next; if ($p != null) $p = $p->next; if ($q != null) $q = $q->next; } if ($carry > 0) { $current->next = new ListNode($carry); } return $ans->next; } }

May 19, 2021 · 1 min · Amer Khalid