Software Development and Programming Careers (Official Discussion Thread)

Rev Leon Lonnie Love

damned mine eyes, DAMNED mine eyes!!
Joined
Nov 11, 2017
Messages
21,906
Reputation
5,468
Daps
88,974
Tried implementing an LRU Cache on Leet Code :shaq2:. I understand the premise but the logic is getting me. Tried 2 approaches, but no luck. HashMap to store entries, a TreeSet to store access times, and a custom object to store key, value, and access to time together (the value of the kvp in the map). I think my misstep with this approach is storing the access times rather than combined object and overriding the get and comparison of the TreeSet.

If it's overrided to sort by access time, the first element is always the LRU. And the get, pass the key rather than the object... Idea vs implementation is a bytch.
Here is my implementation from when I attempted this earlier this year:
Code:
from collections import OrderedDict

class LRUCache:

    def __init__(self, capacity: int):
        self.capacity = capacity
        self._cache = OrderedDict()

    def get(self, key: int) -> int:
        if key in self._cache:
            self._cache.move_to_end(key)
            return self._cache[key]
        return -1

    def put(self, key: int, value: int) -> None:
        if key not in self._cache:
            if len(self._cache) >= self.capacity:
                self._cache.popitem(last=False)
            self._cache[key] = value
        else:
            self._cache[key] = value
            self._cache.move_to_end(key)

It took 172ms to run on python. Its a pretty standard an known approach among the 3 I have come across.
 

Obreh Winfrey

Truly Brehthtaking
Supporter
Joined
Nov 18, 2016
Messages
20,852
Reputation
25,252
Daps
131,941
Here is my implementation from when I attempted this earlier this year:
Code:
from collections import OrderedDict

class LRUCache:

    def __init__(self, capacity: int):
        self.capacity = capacity
        self._cache = OrderedDict()

    def get(self, key: int) -> int:
        if key in self._cache:
            self._cache.move_to_end(key)
            return self._cache[key]
        return -1

    def put(self, key: int, value: int) -> None:
        if key not in self._cache:
            if len(self._cache) >= self.capacity:
                self._cache.popitem(last=False)
            self._cache[key] = value
        else:
            self._cache[key] = value
            self._cache.move_to_end(key)

It took 172ms to run on python.
Nice and simple in Python. I saw somebody had a similar Java implementation as well. This is one of those things where I need to identify if it'd a solution I can tackle more easily in another language. I don't spend a lot of time in Python though, so even though I can come up with some solid Python scripts, trying to do it in an interview will trip me up.
 

Dr. Acula

Hail Hydra
Supporter
Joined
Jul 26, 2012
Messages
25,782
Reputation
8,591
Daps
136,827
I had the pleasure of starting with people with more years in the industry (and at the company) than I had been alive. I felt much the same way but the reality is that the bulk of those people you're working with are very good in a limited area of expertise. There's also the chance that those people are totally inept - and after a few months that will become more apparent. Soak up the knowledge you can, don't be afraid to ask for help, know that even veterans have things to learn. I was the new kid on the block learning and teaching Docker and Kubernetes to people who had hands on Linux in the early days. Towards the end, I had as good or better of a grasp on our platform than some of them did.

Take your time, don't psych yourself out, it'll pass.
So figured I'd update.

So, I can feel the imposture syndrome slowly fading and starting to feel more comfortable. Partly because of what you said in bold. I'm working on some code another engineer touched before me and I wouldn't say he is inept but now I'm starting to feel more comfortable in saying " you know, I would have done this differently or this can be done a bit better that way". It takes a while to just feel relaxed and comfortable enough to realize newbie jitters and insecurities are more to being new than ability. At least for me anyways.

I'm also starting to get more comfortable with the framework simply because I've literally spent like a week just dealing with all the nuances of dependencies, the IDE, and errors of dealing with this particular frame work. That shyt will have you stressed the fukk out.

I'm still feeling like a small fish in a big pond but I can feel myself getting a bit bigger. I think moving to a new city and then immediately starting a new job literally 2 days later had my stress levels off the chart and fukking with my brain.
 

Obreh Winfrey

Truly Brehthtaking
Supporter
Joined
Nov 18, 2016
Messages
20,852
Reputation
25,252
Daps
131,941
So figured I'd update.

So, I can feel the imposture syndrome slowly fading and starting to feel more comfortable. Partly because of what you said in bold. I'm working on some code another engineer touched before me and I wouldn't say he is inept but now I'm starting to feel more comfortable in saying " you know, I would have done this differently or this can be done a bit better that way". It takes a while to just feel relaxed and comfortable enough to realize newbie jitters and insecurities are more to being new than ability. At least for me anyways.

I'm also starting to get more comfortable with the framework simply because I've literally spent like a week just dealing with all the nuances of dependencies, the IDE, and errors of dealing with this particular frame work. That shyt will have you stressed the fukk out.

I'm still feeling like a small fish in a big pond but I can feel myself getting a bit bigger. I think moving to a new city and then immediately starting a new job literally 2 days later had my stress levels off the chart and fukking with my brain.
That's good, and given a little more time you'll forget why you ever felt that way. Inept is too strong a word in most cases, however some developers are very siloed with their knowledge and the minute you try to move them beyond that, you shatter their world. People like that are why I do what I can to understand all of the application. I don't want to be tunnel visioned, and when problems arise I want to be able to intelligently troubleshoot.

One thing you have to try to keep in mind is that you know more than you think you do - it may take some effort to dig up that knowledge - but you're capable. A few weeks ago I was driving headfirst into a framework with no documentation, trying to swap out the technology it uses. I kid you not, I spent about 2 days looking at one class, maybe 50 or 60 lines of code trying to understand what was going on. Eventually I was able to get a foothold and see that while ugly, it wasn't terribly complex.
 

Pyrexcup

Superstar
Joined
Dec 30, 2012
Messages
4,746
Reputation
765
Daps
14,814
Reppin
NULL
apart from leetcode what else is a good resource to expense through work? currently grinding out codewars python challenges but will be expensing a leetcode account before end of the year
 

Obreh Winfrey

Truly Brehthtaking
Supporter
Joined
Nov 18, 2016
Messages
20,852
Reputation
25,252
Daps
131,941
apart from leetcode what else is a good resource to expense through work? currently grinding out codewars python challenges but will be expensing a leetcode account before end of the year
See if you can expense a LinkedIn Learning subscription, possibly some Azure trainings and certification tests from Microsoft.
 

desjardins

Superstar
Joined
Nov 3, 2015
Messages
16,656
Reputation
946
Daps
61,806
Reppin
Mustard Island
Back on my leetcode grind. Alot of concepts I struggled with last time around seem more reasonable now
Reading thru DDIA and getting my mind blown every chapter :dwillhuh:
 

Pyrexcup

Superstar
Joined
Dec 30, 2012
Messages
4,746
Reputation
765
Daps
14,814
Reppin
NULL
apart from leetcode what else is a good resource to expense through work? currently grinding out codewars python challenges but will be expensing a leetcode account before end of the year
just got a year leetcode sub paid for need to find something more now
 
Top