# /daily leetcode questions

date: Mon Sep 19 2022 00:00:00 GMT+0000 (Coordinated Universal Time)

# A Leetcode question a day keeps the impostor syndrome away

I've been doing a fair bit of self-reflection on my programming skills recently, and I've come to the conclusion that I absolutely suck when it comes to theoretical problem solving.

Like really, I can barely finish the majority of Medium questions on Leetcode, let alone Hard. I thought I was smart but apparently I still have a lot left to learn. To be honest, I should've known this when I encountered the 1000th subarray question and had no idea what to do.

As such, I think I'm going to start doing something like a Leetcode question daily, if nothing else but to just keep me thinking. Hopefully it will improve my ability to critically think and determine best solutions for DC&A problems, since I seem to be lacking on that front.

I'll do my best to do one daily, but I would never promise that to myself, because I know I'll break it. But I'll at least try. I'm gonna use my completely dead archive to keep track of my progress.

To start, I'll put down a few of my previously solved questions here.

## 28. Find the Index of the First Occurrence in a String

Medium

Given two strings `needle` and `haystack`, return the index of the first occurrence of `needle` in `haystack`, or `-`1 if `needle` is not part of `haystack`.

``var strStr = function(haystack, needle) {    let searchFor = needle || "";        if (searchFor === "") { return 0; }    if (haystack === "") { return -1; }        for (let i = 0; i < haystack.length; i++) {        let char = haystack[i];        if (char == searchFor) {            let potential = [i, i + (needle.length)];            if (haystack.substring(potential, potential) == needle) {                return potential;            } else {                continue;            }        }    }    return -1;};``

## 35. Search Insert Position

Easy

Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You must write an algorithm with `O(log n)` runtime complexity.

I remember this one took me forever to do, but honestly I don't remember why. My answer is obviously a super simple recursive approach.

``var searchInsert = function(nums, target, idx = 0) {    const currIdx = idx;    const nextIdx = idx + 1;        if (target < nums[currIdx]) {        return currIdx;    }        if (currIdx == nums.length) {        return currIdx;    }        if (nums[currIdx] == target) {        return currIdx;    }        return searchInsert(nums, target, nextIdx)};``

## 4. Median of Two Sorted Arrays

Hard

Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.

The overall run time complexity should be `O(log (m+n))`.

Honestly I think using `sort` here is a bit of a copout, but the question didn't specify writing my own sorting algorithm, so I just let the V8 engine decide what to do.

``var findMedianSortedArrays = function(nums1, nums2) {    const arr = [...nums1, ...nums2].sort((a, b) => a - b)        let midpoint = arr.length / 2;        if (Number.isInteger(midpoint)) {        return ((arr[midpoint] + arr[midpoint - 1]) / 2);    } else {        return arr[Math.floor(midpoint)];    }};``