Thank you for all the effort you put in this comment! That's definitely an interesting path methodology you've laid out. I will give it a try. Would you be willing to talk more as I go through the process?
Great!. Then I can give a few of my thoughts, I wouldn't say "advice". But you can have a think about it.
I did Computer Engineering degree, which is different to Computer Science. At the time I have always thought how CS would be a much better degree, but I could not get in due to higher entrance requirement, so instead I got into CE. I dropped into the CS class whenever I can. Reading their teaching materials, I grinned. I remember thinking why do they teach outdated stuff like these. Perl! ( It was on the way out, the future is super hyped thing called Java ). May be because my Collage wasn't all that good in CS ranking. May be that is why. In CE, we actually do lots of what I call real engineering work, with transistor and low level design. Which requires lot of discipline, and rules to follow.
After nearly 20 years. My thoughts are, none of the CS degree preps you for today's working environment. And it never will. It doesn't matter which college you go to, 90% of those work aren't really related. The modern tech teaching is not outdated, it is just the hype cycle moves so fast and tech industry as a whole ( Actually not tech industry, but specially Silicon Valley ) likes reinvent the wheel every few years what was put into text book material are likely outdated.
Most of the best programmer I have met, actually not most, but ALL of them. Are self taught.
And what is funny, ( sorry if this hurt anyone ), most programmer have absolutely No idea what the heck they are doing. They somehow got a pieces of code working, compiled, and it works. Don't ask them why or how or if it is good. At the moment they would have thought those code are good enough, likely 2 years later they thought it was a pile of crap.
Compared to real software engineering, which for example, code used by NASA, or Sqlite, the amount of testing and thoughts required are insane. But those are niche cases in today's market.
Then there is the domain knowledge, ask DHH to explain Linux Kernel to you? He would have said who the fxxk understand this. Algorithm what? Ask Linus to do Web Programming? Who the Fxxk has invented these pile of abstracted mess and trillion of framework just to render a page?
I was the few of the best in class for my programming project. But I never got a programming job, mainly because I had always in my mind, that real programmers were like Swordfish, Matrix, whatever thing that type so fast and could solve a problem instantly. But instead, now I know most programmer just stare at the monitor and does JACK ALL for a whole day.
Programming is 80% Reading + Thinking. Actually I will go on to say 90%. For a 100min of work, you only spend 10 min typing it out. There is actually lot of thinking going on without you realising it, when you have lunch, dinner, sleeping. Your Brian is constantly working in a background thread without you even knowing it.
I wish I had known this at the time. I wish someone could tell me programming is like that. I could happily hack out a solution in my own time and schedule, but I cant think faster and solve a problem in mins.
I had always dream of telling others; the world outside geek circle, how programming really is. I had always wish someone could fund me doing a Mini film, and have it played in all university or programming courses. It will be a documentary of famous programmers every day work and programming. From Facebook, Google, Apple or Microsoft. Having them staring at the monitor, and fast forward showing they did zero typing of code. And has embarrassing question that should know but they don't. How all these programmers are really no different to you.
I know how it feels, programming at your own schedule vs programming at someones else scheduled when you have no idea how long it will take. Would I be fired if I didn't work fast enough?
Programming isn't for everyone. There is a difference between able to write program and likes to write program for a living. And finally you are young! Explore more! Unless you have a burden ( bring money home ), keep looking, don't settle. I didn't understand the explore options when I was young, but once you are in your 30s, your freedom to explore are limited by families, relationship etc.
I enjoy it, but not given the failure rate I'm experiencing. If I had 50% or less failure ratio I would be ok. But as is essentially every problem is a 2-5 hour struggle where I meander to an answer feeling usually I temporarily learned why X didn't work in that given situation and not something widely applicable.
My view has been they are looking for people with high IQs exclusively. Whiteboard interviews cut out people who are not even intelligent enough to memorize solutions and algorithms while also accepting people smart enough to come up with solutions to tough problems they haven't seen before. It seems given how much I struggle I may not even fit in the memorize 300+ leetcode category.
I'm very interested in this divide between people who are for white board interviews and against it and how it correlates with job performance and content. I may make a separate post about it because I'd like to get to the bottom of it.
For the median question, I did not allow myself to combine the array. My idea was to increment in each array based on which value would come next in a sequence (as if they were on array), stop when I've incremented n times where n = length of both arrays / 2 (the median position) and then return. Here's the code:
length = len(nums1) + len(nums2)
medianIndex = length // 2
ptr1 = 0
ptr2 = 0
curr = 0
flag = 0
if nums1[ptr1] < nums2[ptr2]:
val = nums1[ptr1]
else:
val = nums2[ptr2]
while curr < medianIndex:
if nums1[ptr1] == val:
if nums2[ptr2] < val:
curr += 1
ptr1 +=1
val = nums1[ptr1]
flag = 0
else:
if ptr1+1 < len(nums1) and abs(nums1[ptr1+1]) - abs(val) < abs(nums2[ptr2]) - abs(val):
curr+=1
ptr1+=1
val = nums1[ptr1]
flag = 0
else:
curr+=1
val = nums2[ptr2]
flag = 1
else:
if nums1[ptr1] < val:
curr += 1
ptr2 +=1
val = nums2[ptr2]
flag = 1
else:
if ptr2+1 < len(nums2) and abs(nums2[ptr2+1]) - abs(val) < abs(nums1[ptr1]) - abs(val):
curr+=1
ptr2+=1
val = nums2[ptr2]
flag = 1
else:
curr+=1
val = nums1[ptr1]
flag = 0
if flag == 1:
med = nums2[ptr2]
else:
med = nums1[ptr2]
if length % 2 == 0:
if med - nums1[ptr1 + 1] < med - nums2[ptr2 +1]:
return (med + nums1[ptr1+1])/2
else:
return (med + nums2[ptr2+1])/2
else:
return med
Here's what I wrote for your sum the cubes. Took me 1 minute. I hope I didn't misunderstand the question.
def sumCubes(n):
sumC = 0
for i in range(n+1):
sumC += i 3
return sumC
I'll give you 2 more leetcode examples. Neither passed all test cases. The first is 2-3 hours. The latter 3. I didn't post the last submission for the latter one because I believe the one I posted is more telling of my thought process and coding ability imo so that solution is between 1 and 2 hours of work, but I wasn't able to get it in the end.
I honestly don't recall my idea here. I think it was to see if the start and end of the array had the same letter and if it didn't cut off the first and last letter and continue analyzing the array until the center is reached.
class Solution:
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
curr = ""
big = ""
for c in s:
curr += c
if curr != curr[::-1]:
if len(curr[:len(curr)-1]) >= len(big):
big = curr[:len(curr)-1]
print(big)
while curr != curr[::-1]:
curr = curr[1:]
if len(curr) > len(big):
return curr
else:
return big
I thought there were 3 cases in this problem. You hit a character, you hit a ? or you hit . The first one meant an exact match had to be made. The second that there only had to be some character in the string to take a position (the string couldn't end if there was a ?). The last could mean 0 to n matches so there had to be a lot of checks in my mind. I also thought about what happens if there's "?" which I wrote as a case inside the "" if case. When test cases didn't work I would try and code for that test case and it kept going wrong so I eventually gave up and looked at the solution. My main flaw is that it didn't occur to me that backtracking was the way to go or a way to check all the potential combinations. I thought you just had to keep moving forward.
class Solution:
def isMatch(self, s, p):
"""
:type s: str
:type p: str
:rtype: bool
"""
if len(p) == 0 and len(s) == 0:
return True
if len(p) == 0:
return False
i = 0
j = 0
while i < len(s):
if j >= len(p) and p[len(p) - 1] != '*':
return False
if p[j] == '*':
while j < len(p) and p[j] == '*':
j+=1
if j == len(p):
break
elif p[j] == '?':
x = len(p)- j
while len(s) - i > x:
i+=1
while j < len(p) and p[j] == '?':
i+=1
j+=1
if i > len(s) or j >= len(p) and i < len(s):
print(i)
return False
if i == len(s) and j == len(p):
break
else:
while i < len(s) and p[j] != s[i]:
i+=1
if i == len(s):
return False
elif p[j] == '?':
i+=1
j+=1
else:
if p[j] != s[i]:
print(i, s[i])
print(j, p[j])
return False
i+=1
j+=1
if j < len(p):
while j < len(p) and p[j] =='*':
j+=1
if j == len(p) and p[j-1] == '*':
return True
else:
return False
return True
This is what I tried, recursion taking care of puting the simplest cases first. I haven't checked the solution. I think this kind of problem is like a state machine. In each call of the recursion the length of the pattern or the lengh of the string should decrease, so finally one get to the easy cases of length in (0,1,2).
This kind of problems are designed to test you can define a grammar and apply recursion. Another problem with the same flavor could be to evaluate string expressions like "3+25" using a grammar. My one-hour solution (not checked). But I think I have done similar problems.
def pat(s,p):
...: if s == "":
...: if len(p)>1 and p[1] in "?*":
...: return pat(s,p[2:])
...: if p == "": return s == p
...: if len(p) == 1:
...: if p == "*": return True
...: return s == p
...: if len(p) == 2:
...: if p[1] == "?":
...: return s == p[0] or s==""
...: if p[1] == "*":
...: if s[0] != p[0]: return False
...: if s == p[0]: return True
...: return pat(s[1:],p)
...: if p[1] != "?" and p[1] != "*":
...: return s[0] == p[0] and pat(s[1:],p[1:])
...: if p[1] == "?":
...: if (s[0] != p[0]):
...: return pat(s,p[2:]))
...: if (s[0] == p[0]):
...: return pat(s[1:],p[2:]) or pat(s,p[2:])
...: if p[1] =="*":
...: if (s[0] != p[0]): return pat(s,p[2:])
...: return pat(s[1:],p) or pat(s,p[2:])
...:
...:
I see that you prefer not to use recurrency in Python. That decision is correct since python is not optimized for recurrency but it also can make the code more complex. See https://realpython.com/python-thinking-recursively/
In one of your comments you say you have improved a lot in two months or so, so perhaps you need a little more practice and I think you could improve a lot more (less time for solving puzzles and writing better code). Anyway, I have tried to solve some of the puzzles. Perhaps other people in this thread can give more valuable information about your coding style. I wish you the best.
Sum of cubes:
sum(i**3 for i in range(n+1))
findMedianSortedArrays using Haskell:
let m a [] n = a !! n
m [] a n = a !! n
m (x:_) (y:_) 0 = if x<=y then x else y
m (x:xs) (y:ys) n = if x<=y then m xs (y:ys) (n-1)
else m (x:xs) ys (n-1)
let m1 a b = if odd n1 then m a b n12
else (m a b n12 + m a b (n12-1))/2
where n1 = length a + length b; n12 = div n1 2
let findMedianSortedArrays = m1
I haven't checked the code.
You're not wrong that there's a mental health issue at hand. There's also a lot riding on my ability to do these leetcode questions. I've been talking to councilors for the past decade, but I'm at a point where I need to be financially independent. I wrote more about it in this comment:
But why do you want to work somewhere where you have this grotesquely irrelevant gauntlet? A silly interview process doesn't mean it's a good job or a good company, it just means they've got more applicants than they know what to do with.
I know there's a lot of pressure here to work a 80h week but you can be financially independent without pressure. Just pick your battle.
You've got a skill. Believe it or not, programmers are needed all around the world. The pay is dramatically variable but so are living costs. Move. Find a nice town with a small software company that's hiring and start a life. Get your head straight. Read. Get a hobby. It's all much easier when you're not blowing $3k/month on a 1 bed studio.
You can also work shunt jobs directly for companies. Build websites, build apps. Lots of project management and quoting for things that never come to fruition but it's a job and very little overhead.
I'm not trying to put you down. I'm sure you can jump start you happiness the way you're desperately trying to... But stepping into a low pressure job somewhere where it's dirt cheap to live is a much healthier approach to the problem you have.