## LeetCode 92. Reverse Linked List II

I copied solution for Reverse Linked List II from this Udemy course. It was a little hard to understand all these pointers in second loop, I had to use pen and paper to draw each step to finally understand how each pointer works.

``````/**
* function ListNode(val, next) {
*     this.val = (val===undefined ? 0 : val)
*     this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {number} left
* @param {number} right
* @return {ListNode}
*/
var reverseBetween = function(head, left, right) {
let currPos = 1;
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 newList;

};
``````

## LeetCode 206. Reverse Linked List

Here is a quick solution to Reverse Linked List problem.

``````/**
* function ListNode(val, next) {
*     this.val = (val===undefined ? 0 : val)
*     this.next = (next===undefined ? null : next)
* }
*/
/**
* @return {ListNode}
*/
let prev = null;

while (curr) {
let next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}

return prev;
};
``````

## 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.

This review is from the perspective of someone who gets easily motion sick.

Luckily, the games with no forward motion does not cause any nausea; games like Beat Saber, Thrill of Fight are perfect for me. These games are rated as Comfortable in their Comfort ratings.

I tried a few games in which there was a walking motion, and those games made me almost throw up.

One of technical complaint I had read was that Field of View is narrow on Quest 2. If you look for it, yes it is really narrow. But when I am in the middle of game, I hardly notice it. It is very immersive experience and narrowness of FOV is not really a concern.

As for workout, the two games I bought, Thrill of Fight and Until You Fall, are probably providing enough cardio. My heart rate goes pretty high. But as I sweat, it fogs inside. I wear glasses and I think it mostly my glasses getting fogged. I ordered VirtuClear prescription lenses, and will see if they perform any better.

A lot of people complain about standard straps but I haven’t had any issues.

VR videos tend to make me motion sick, especially if there is any camera movements. Videos with stationary point of view are okay. I wish videos had similar Comfort level ratings and you can filter them. Really hate it when a video starts out good but then there is sudden camera pan that makes me want to throw up. VR certainly will change how movies are directed in the future.

There are also interactive game like video experiences. These are interesting and tend to have no camera movements. Notes on Blindness is pretty good.

There are a lot of videos and apps for facing your fears like height and insects. Some of them have cartoon-like graphics but still trigger fear response like trying to cross bridge in one of Quills animations.

Another thing I love about Quest/VR is just chilling in relaxing environments. Like I literally sit, enjoy scenery, and do nothing. Immersion is great if you are not pixel peeping as they say in photography world.

Overall, I think Quest 2 is great, and VR has come long ways in last 2 years. It does provide immersion and should only get better.

## Importing plotly failed. Interactive plots will not work.

Fix:

``````pip3 install --upgrade plotly
``````

## 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.

Memory Usage: 46.6 MB, less than 81.14% of JavaScript online submissions for Subarray Sum Equals K.

## 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
*/
\$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;
}
}
``````

## Docker in 5 minutes

I have been using Docker and Kubernetes for several years now but never really took any time to really read about it. There were a lot of gaps in my understanding of how Docker works and what really is possible with it. Just recently my employer started to offer Udemy’s subscription, so I took Hands on Docker course. Here are some of the important things I learned in this course.

## Where do you run Docker containers

You run these in Windows or MacOS but most likely you will use Linux in production. And unlike Windows and MacOS, you can run Docker on Linux VPS.

## Important Docker commands

``````# get running docker containers
docker ps

# get all docker containers including those that have exited
docker ps -a

# stop container by its id or name
docker stop first_few_chars_of_id

# list docker images stored on the host
docker images

# remove image
docker rmi image_name

docker pull image_name
docker pul image_name:specific_tag

# append a command
docker run image_name command to run with parameters
docker run ubuntu sleep 5

# execute command in a conatiner
docker exec container_id cat /etc/hosts

# interactive session inside container
docker exec -it conatiner_id /bin/bash

# run container in background/daemon mode
docker run -d webapp

# tail logs
docker logs -f conatiner_id

# attach to conatiner running in detach/daemon mode
docker attach container_id

# map ports, for example, to map port 80 on host to port 8080 in container
dokcer run -p 80:8080 webapp
``````

## LeetCode 7. Reverse Integer

My initial solution for Reverse Integer problem:

``````/**
* @param {number} x
* @return {number}
*/
var reverse = function(x) {

let ans = '';
if (x<0) {
ans = '-';
x*=-1;
}

str = x.toString();

for (let i=str.length-1; i>=0; i--) {
ans += str[i];
}

ans = parseInt(ans);
if (ans>2**31-1 || ans<(-2)**31) {
return 0;
}
return ans;
};
``````

## Results

Runtime: 104 ms, faster than 34.78% of JavaScript online submissions for Reverse Integer.

Memory Usage: 40.5 MB, less than 33.80% of JavaScript online submissions for Reverse Integer.

## Optimized Version:

After looking at solution and transpiling it to JavaScript:

``````/**
* @param {number} x
* @return {number}
*/
var reverse = function(x) {

const max = 2**31-1;
const min = (-2)**31;
let ans = 0;

while (x != 0) {
const pop = x%10;
x = Math.trunc(x/10);
if (ans > max/10) return 0;
if (ans < min/10) return 0;

ans = ans * 10 + pop;
}

return ans;
};
``````

## Optimized Results

Runtime: 92 ms, faster than 87.08% of JavaScript online submissions for Reverse Integer.

Memory Usage: 40.4 MB, less than 33.80% of JavaScript online submissions for Reverse Integer.

## To Sufi by Allama Iqbal

I translated this poem in my freshman year for my English class.

This is a world of miracles for you.

But for me, this is the world of randomness.

The world of fantasies is poor,

But poorer is the world of births and deaths.

It is no wonder that your gaze will change it all,

The world of possibilities is calling you.

## Stars and Moon

This poem was written by my favorite poet Allama Iqbal. I translated it for my English class during my freshman year.

“The view up here is the same as ever.

We are tired of shining and shining.

Our work is to walk, day and night.

Walk and walk and walk forever.

Everything in this universe is anxious.

Peace, whatever it is, does not exist.

Everything is being tortured by time.

Stars, man, trees, rocks, Everything.

Will this journey ever end?

Will we ever see our destination?”

“My friends,” replied the moon,

“O travelers in the field of the night.

Life happens because of the movement.

This is the old culture of this universe.

Time’s horse flies.

Because it is hit by the lash of desires.

On this path, it is not possible to stop.

Because death is hidden within rest.

Those who strove have left us behind.

And those who slept are crushed.

The end of this journey is true beauty.

It begins with true love and ends with true beauty.”