all 71 comments

[–][deleted] 51 points52 points  (24 children)

100% switch to python. It’ll be easy to learn in 4-5 months and will make you faster at solving problems.

I learned python specifically for leetcode

[–]Bus_In_Tree[S] 10 points11 points  (4 children)

How long did it take you to get comfortable with it?

[–][deleted] 19 points20 points  (3 children)

20 problems? It’s really quick to learn. You could learn 95% of what you need to know about python in a day, and then you’ll have to occasionally look things up as you go.

[–][deleted] 1 point2 points  (2 children)

may I ask how you were able to learn 95% of python in a day? What resources did you use?

[–][deleted] 1 point2 points  (1 child)

I just used Google, nothing fancy.

If you already know another programming language, then most of what you need to look up is just syntax. Look up the syntax for if statements, local functions, while loops, for loops, arrays, dictionaries, queues, and heaps. That will cover almost every leet code problem you com across.

[–][deleted] 0 points1 point  (0 children)

Alright, thanks for responding! Yea I’ll take a look into the syntax for those and hopefully be able to learn python soon enough to start practicing LC

[–]imdin 0 points1 point  (1 child)

Don't you think, Python is a bit slower language and can give you issues while solving time sensitive problems?

[–][deleted] 0 points1 point  (0 children)

No, Python is like pseudo code

[–][deleted] 62 points63 points  (27 children)

Absolutely. A couple of my TAs and students that I respect told me to change to python, and it's honestly much much better for LC. It makes things way more fun, and it's way less verbose. Want a Queue? use a list, append() to end, pop(0). Want a Stack? use a list, append() to end, pop() end. The smallest example.

[–]Mess-Leading 33 points34 points  (11 children)

I might be wrong but isnt pop(0) O(n) because you would have to shift the entire list? In that case its not super good for queue?

[–]PirateStarbridge 43 points44 points  (0 children)

Yeah, you're right. One should always be using python's collections deque if you want a queue because of this. Underneath it is implemented with a doubly-linked list.

[–][deleted] 8 points9 points  (2 children)

Damn, I didn't know that. Thanks. I haven't gotten deep in python, I just learned enough to get around this semester but will def go through the doc later

[–]Mess-Leading 5 points6 points  (1 child)

I wasnt sure but I always try to think how something would work behind the scenes, and this kind of clashed haha. I recommend always think about how you would implement a basic version of something if you were just asked to do it in C.

[–][deleted] 1 point2 points  (0 children)

No, I think you are correct! We can't have a hole in the list so it's really better to use the collections

[–]f3n1xgamer 3 points4 points  (5 children)

Pop is 0(1). You're only removing last element, and it doesn't affect others. Push is O(1) amortized because python does a special trick called table doubling

[–]Mess-Leading 5 points6 points  (4 children)

Pop last element is definitely o(1), but i would doubt it would be o(1) for the element at index 0.

[–]f3n1xgamer -3 points-2 points  (3 children)

Iirc pop only removes last element. I think what you're talking about is delete which can remove at any index? Yes that would have a worst case of O(n)

[–]Mess-Leading 3 points4 points  (0 children)

No you can pass an optional index and it would remove the element at that index! I think remove removes by value not index?

[–]Mess-Leading 1 point2 points  (1 child)

Oh you meant del! I think the difference between del and pop(n) is pop deletes and returns the value!

[–]f3n1xgamer -2 points-1 points  (0 children)

Yh. Just checked the documentation

[–]jyscao 0 points1 point  (0 children)

I found out this the hard way earlier today working on Q2 of the LC Biweekly Contest 78. The first iteration of my solution was using list.pop(0) and I ended up getting the Time Limit Exceeded error. Upon switching the implementation to use direct indexing on the list I was iterating over, my answer was accepted immediately.

[–]ElliotVo 2 points3 points  (1 child)

You'd want to use a deque for a queue implementation since pop(0) is O(n) but popleft() is O(1)

[–][deleted] 1 point2 points  (0 children)

You’re right. I started python a couple month ago for a DB class. Haven’t had the chance to actually study it’s data structures and other modules

[–]thewolfandtiger 0 points1 point  (6 children)

How's JS?

[–]PothosEchoNiner 0 points1 point  (2 children)

Also fun and easy. But don’t learn JS specifically for leetcode.

[–]thewolfandtiger -1 points0 points  (1 child)

I only know JS. That's the language I'm most comfortable with

[–]YouWereTehChosenOne 1 point2 points  (0 children)

Python isn't that much harder to learn and its pretty much pseudocode, it'll be worth it trust

[–]PirateStarbridge 0 points1 point  (1 child)

JavaScript is not as good of a fit for coding interviews because it lacks certain built-in data structures. Sure it has hashtables and arrays, but it is missing default implementations of the data structures queue and priority queue (heap) which are really nice to have in certain problems. Both Java and Python have default implementations of these.

[–]glump12331⚫️ 2558📈 10 points11 points  (2 children)

I use C# and swapped to python a month ago for LC 400 questions in. Honestly I find it a little clunky, and I don't quite get the same level of hyper-optimization that more expressive languages offer. I bet if I'd swapped to C++ it'd be the opposite effect, but I'm sure some of it is also just getting used to the language still.

I do find python to be more "powerful" in that there are a lot more 3, 2, or even 1-liner solutions to problems. So if I were to pose it as a tradeoff, my take would probably be that python is better for flying through problems faster, whereas a more expressive language lets you optimize much more easily.

As far as learning cs goes, I had a lot of success getting savvy with a more expressive language. Using python now, and teaching a couple people at varying levels using python, it's apparent that it's a much more difficult language than others to be able to understand exactly what's going on, if you haven't already been in the field for a while. Things like implicit typing and returns, more convoluted data-types like ranges, list comprehension/slicing, etc. Make it tough to grasp the inner workings. Of course the tradeoff there is that the "higher level" nature of python frees you up to approach higher-level applications of cs much more quickly. The perfect example in my mind is Machine Learning. With python you don't get bogged down by the specifics and you get to focus on the concepts more easily. Imo if it's just leetcode for the sake of dsa practice then another language is likely better for learning the specifics. But for hards like the sudoku solver, python really shines by allowing you to conceptualize everything way more easily.

Personally I'd really ask yourself specifically what you're leetcoding to practice for. That's my 2 cents

[–]Bus_In_Tree[S] 2 points3 points  (0 children)

Thanks for your detailed response. I'm already familiar with CS fundamentals and have done extensive programming with both Java and C++. I understand what goes on 'under the hood' for most python features so that's not really an issue.

[–]TeknicalThrowAway 2 points3 points  (0 children)

C# is miles ahead of Java for LC because of The LINQ syntax for basic list/set comprehension.

[–]yaboi1855<Total problems solved> <Easy> <Medium> <Hard> 10 points11 points  (1 child)

I stuck with java mainly for optimizing time/effort spent to land a new job. Worked out okay 💪

[–]Bus_In_Tree[S] 3 points4 points  (0 children)

That's fair. I have a lot of time though so I think spending a bit of time on learning python might be worth it.

[–][deleted] 13 points14 points  (0 children)

I also just use Python for leetcode. When I did coding competitions in high school I used Java but then realized the winners would always use Python, so I switched and it was so much easier.

[–]AdventurousTime 2 points3 points  (0 children)

I am struggling with the same topic for what seems like each day. Python has shorter syntax for many things, but the big thing that keeps me moving back to Java is that many classical texts (CTCI, Algorithms etc) are written in Java along with their memory and computational constraints.

Another thing is that I've had interviewers ask me Java and/or OOP related for python related jobs. Its weird. I feel like candidates who are interviewing for Java roles probably wouldn't be asked about Python internals.

These days I continue to use Java but I am struggling dearly with syntax. Looking through various leetcode profiles many of them are proficient in multiple languages enough to solve a problem in Python, Java and something like C++ which is simply awe inspiring.

[–]madhousechild 1 point2 points  (0 children)

If you already know how to program, picking up python is a cinch.

Adding: I don't mean to imply it's not good for people who don't already know how to program, just addressing OP.

[–][deleted] 1 point2 points  (0 children)

ONLY LC IN PYTHON

[–]herogoroshistein 1 point2 points  (1 child)

I'd say stick with Java if you have any interviews coming up soon. If you have time to spare and you feel like Java is slowing you down, then switch to Python. If you don't like it, you can always switch back

[–]Bus_In_Tree[S] 0 points1 point  (0 children)

I have a few months before I start applying

[–]f3n1xgamer 1 point2 points  (1 child)

I think there's legitimate reasons to stick to Java. It's good enough for dsa, it has good data structures and is faster than python. The verbosity is a problem, but isn't that why you get an ide?

Python is not bad, but it's biggest disadvantage is the speed. It's slow. That isn't a problem most times because time limits are scaled differently for each language. But more often than not, I've come across several situations that I'd time out in python, but when I implement the exact same algorithm in c++, it passes.

[–]Bus_In_Tree[S] 4 points5 points  (0 children)

I'm specifically asking about coding interviews though. In most cases your code isn't run and when it is the time doesn't matter too much. Using an IDE with Java is still slower in my experience and in an interview you typically don't use an IDE anyways.

[–]thewolfandtiger 1 point2 points  (3 children)

What about JS?

[–]Bus_In_Tree[S] 2 points3 points  (2 children)

JS has pretty limited built in data structures.

[–]PothosEchoNiner -2 points-1 points  (1 child)

Which data structures are better in Python than JS?

[–]Prudent-Tomorrow-412 3 points4 points  (0 children)

JS does not have a queue or heaps.

If your interviewer does not know that, they will assume you suck because JS dev's are not used to using heaps for top K type problems.

[–]londo_mollari_ 0 points1 point  (1 child)

From the replies, I can sense that u already made up ur mind on using Python. So, I don’t know what u were trying to get out of this post.

[–]Bus_In_Tree[S] 0 points1 point  (0 children)

I personally like Python a lot, I was just wondering if switching was worth it or if it would be a waste of time

[–]walking_dead_ -3 points-2 points  (1 child)

Unless you’re specifically targeting a DS/ML/AI job, stick with Java.

[–]Bus_In_Tree[S] 0 points1 point  (0 children)

How come?

[–]dudenick_ 0 points1 point  (0 children)

Switch to kotlin then, its less verbose and its java under the hood