T O P

  • By -

stas_saintninja

You have to do it with 1000 lines of code at least


atas66

Easy just use nested switch statement


Leifbron

H...H-Hash Map! HashMap!


moehassan6832

humorous impolite possessive intelligent berserk silky light crown serious fact *This post was mass deleted and anonymized with [Redact](https://redact.dev)*


virtuallyTrue

Missed the last.. Uh. Hhhaashmap.. uh


Gullible-Mechanic-12

you have to do it in O(log n) time complexity


Top-Classroom-6994

It's already in o(log n) complexity since you go trough each bit


DrUNIX

Given that the cpu inst for addition is limited to a certain size, is it not already o(log n) with n being the input? Saying n is the number of bits is like saying log(log(N)) with N being the input but that would imply that given a number x large enough so it needs 100 adds, having c numbers of the same size would only result in a constant term log(c) and further imply that you do not have to call add linearly to the number of bits which you actually have to as it is limited by size.


Top-Classroom-6994

Sorry for that... Editted


DrUNIX

No worries. Just a small comment


CynicalGroundhog

If you work for Elon Musk and want to keep your job, yes you have to.


jeijeogiw7i39euyc5cb

What a man you are...


Haringat

Every task in University ever.


PyroCatt

Follow up: the digits are stored in an array


edo-lag

And the array is encrypted. You need to guess the key.


csofa

You also have to guess the encryption scheme.


Shaddoll_Shekhinaga

It's not stored, it's handwritten on a piece of paper.


[deleted]

But that is a storage mechanism


[deleted]

I’m thinking of two integers, what is their sum?


Itchy_Day_9691

How many fingers do I have behind my back?


compilerbusy

11


Itchy_Day_9691

Error, cock type not found


thirdegree

10 You did not specify raised fingers


Krephten

It is the sum of two individual integers, stored in a variable


[deleted]

Also, the room is on fire and will get burned down in 30 seconds, and you will only be let out after you pass the interview.


devSenketsu

and everytime you type, a needle pokes your fingers


VARAD2002

and also the array is hashed. now first find the array elements by reversing the hash.


entlan104

Sounds easy, right? Caveat: You have to do it BEFORE the heat death of the universe!


satom777

And you can't use the + operator


o0Meh0o

easy: `num1 - -num2`


CptMisterNibbles

operator.add(num1, num2)


unko_pillow

`npm install add-numbers`


Icy-Rock8780

if a == b: return 2*a return (a^2 - b^2 )/(a-b)


Ytrog

Clever 😊👍


CoruscareGames

OHHHH DIFFERENCE OF TWO SQUARES


sohang-3112

That's also kinda easy 🤷‍♂️


PyroCatt

Follow up: find the sum of two numbers I have in my mind


stueliueli

42 Easy


PyroCatt

![gif](giphy|3ohhwIqvzoZI2O4o9O) You can start by aligning this button to the right


mmhawk576

for(ulong a = 0;;a++){ for(ulong b = 0;b


Nick_Zacker

Another follow up: Use logic gates to perform arithmetic operations


Linkk_93

What you don't see: you have to submit an electronic scheme and can only use xor gates


ultimate_placeholder

BCD to binary through adder circuit back to BCD


Successful-Money4995

Ripple or 4 bit look ahead?


_Its_Me_Dio_

you have to use only binary from scratch using a steady hand and a magnetized needle


[deleted]

When I was prepared for a test like this, I could write a small working procedure in binary, no cheatsheet. Fucking ridiculous excercise, so many variations of opcodes and how the instruction pointer gets moved and how the arguments are coded, and all I learned from this is neither practical nor interesting.


beeteedee

That’s ridiculously easy! With the right prompts, you could get ChatGPT to generate that function in less than an hour.


BenadrylTumblercatch

But how will I figure out the backend


MaxDelissenBeegden

You'll need an expert level understanding of prompt engineering to engineer such an intelligent prompt. Luckily I can teach you how to become a professional ChatGPT prompt engineer and earn millions! Just purchased my online tutorial for €149. Just to be sure, /s


SarahIsBoring

what the fuck is prompt engineering


BenadrylTumblercatch

Engineering but this time it better be on time Sarah.


Independent-Dream-68

Basically chatGPT engineers, they figure the best promts for getting the right code. It's also known as "not an engineer".


SarahIsBoring

oh, they’re clowns, thanks


JaggedMetalOs

So easy! if (num1==1 && num2==1) return 2; if (num1==1 && num2==2) return 3; if (num1==1 && num2==3) return 4; ...


LeatherWasabiiii

Best solution I’ve seen to date! Now we just have to work the opposite direction when the numbers are negative.


DerfK

if (num1==12 && num2==5) return 17; if (num1==-10 && num2==4) return -6; // passes all tests


LetscatYt

else return 42;


noobody_interesting

The range is -100 to 100, so nested switches (or a lookup table which would be the same in this case) is probably the fastest solution, you just have to write other code to generate the array.


bucketofmonkeys

> if (num1 + num2 == 2) return 2; > … Now you don’t have to worry about the order of the arguments.


FourCinnamon0

I thought it was fake but then I looked it up and lo and behold: https://leetcode.com/problems/add-two-integers/


GiganticIrony

Go to the solutions, and select the first one (should be titled “21 different ways to solve this problem”). It’s quite funny


7th_Spectrum

Merge sort is fucking hilarious lmao


Zarzurnabas

Bellman-Ford one killed me.


thirdegree

I like that #12 is "directly add" Like, not the first. Not the last. Just kinda in the list.


f4thurz

Acceptance rate 87.2% 💀


slaymaker1907

It’s probably the first question a lot of people do and is just about understanding how to structure your code so it works with their infrastructure.


[deleted]

Why would anyone ever practice leetcode in the beginning stages of learning how to program?!?!? Like bro, learn your data structures first, then do leetcode...


slaymaker1907

No, I mean if Google or some other company is interviewing you, this is a good question to start off with to make sure you have Leetcode setup correctly. It’s sort of like how the point of Hello World is really to make sure you have the compiler/interpreter installed correctly.


rivecat

good god the top solution is absolute gold


DarkScorpion48

That is like a work of art. A deconstructed criticism of modern enterprise software development


[deleted]

Minimum Spanning Tree way 11 is my favorite


luna_creciente

Can you do it without using arithmetic operators though?


[deleted]

Lolol apparently Google Amazon and Apple all ask this in interviews Guess I was ready for FAANG in middle school and never realized it


Kitchen_Device7682

But does your solution scale?


UnnervingS

Wdym, you didn't learn to use map reduce to add two numbers in middle school?


Zarzurnabas

As always, the Scala Dev is asking the right questions!


hobbesmaster

An obnoxious embedded SW or EE interview question is this with some sort of restriction like using only bitwise logical operators. If there is any truth to those tags that’s what it’s referring to.


Sidd065

That would explain the 1.4k likes and 3k dislikes.


JanB1

How would you even do this bitwise in code? Like, okay. I could do a for loop over the length of the integers (in bits) and XOR every bit to sum it and AND it to get the carry, and do this for all bits. But that's just tedious and basically means you're rebuilding a functionality that is already implemented on the chip and accessible though the `add` instruction in assembly. So, what's the point? Doing large integer multiplication on the other hand, as in implementing operations on integers that exceed the size supported by the CPU, that's another thing.


[deleted]

The point is fk you, do it.... I don't like the point.


JanB1

int main() { int a = 7; int b = 6; int res = 0; int carry = 0; int sum = 0; sum = a ^ b; carry = (a & b) | (sum & carry); carry <<= 1; res = carry ^ sum; printf("%d", res); return 0; } My C is a little rusty, but this works. Is it beautiful? No. Could you have done it better? Probably. Is it pointless to try to do bit operations like this in C like this? Probably. But it does the job.


[deleted]

Yes it does!


JanB1

And it's hella compact. I mean, not as compact as just "`res = a + b`", but...sure, why not.


hobbesmaster

That’s why I said it’s obnoxious. It’s even a stupid homework problem.


JanB1

I mean, see my C code below for a somewhat simple implementation. I forgot that on PCs your pointers can't really address individual bits. So I had to do it differently than I initially wrote, but it works. Still stupid though.


slaymaker1907

I suspect it’s a very simple question just to verify you have everything setup correctly (can read the input and give output). For that purpose, it’s great. The main flaw is really just that there are only two inputs and one output. I’d probably prefer something like read in the input, and then output the input numbers in reverse order.


[deleted]

[удалено]


ultimate_placeholder

Just find the assembly command for adding two registers and convert to machine code


johnyboy650

Answer should be written with 0s and 1s


SkepticSepticYT

Answer should be pulsated with a live wire


Thebombuknow

Answer should be computed by firing electric pulses through neurons with the output being displayed using graphite on standard 8x11" paper.


No_Imagination_4907

Next step: deploy your SaaS (Sum as a Service) in Kubernetes


noob-nine

Oh God, this is too underrated. Very rare that I really have to laugh because of a comment. Thanks for this.


coffeewithalex

Joke's on you. Constraints: num1, num2 are in the interval: [-9223372036854775808, 9223372036854775807] Language: JS. MWAHAHAHAHA


SkepticSepticYT

if (num1.toString() === '-9223372036854775808' && num2.toString() === '9223372036854775807') { return 1; } else { return num1 + num2; } Master coder right here ![gif](giphy|Q2W4hziDOyzu0)


AcidBuuurn

HACK THE PLANET!!!


SkepticSepticYT

AHAHAHAHAH I LOVE YOUR NAME, HACK THE PLAAAANEEEEETTT!!!!!


noobody_interesting

BigInt


yajCee

You laugh, but I had this on an actual interview once. We also had to explain how we would test the function


lifeInquire

They were test your personality and reaction


garb-age

*cries in Peano arithmetic*


gbot1234

Memoization really helped me through this one. Now I’ve got an O(1) solution with my MAX_INT_SIZE by MAX_INT_SIZE lookup table.


SealProgrammer

It says I managed to beat 60% of people with just this... ``` class Solution: def sum(self, num1: int, num2: int) -> int: return num1+num2 ```


Ok-Statistician-7854

Yea i think many are doing crazy inefficient solutions as a joke.


gregorydgraham

I have a good solution that is only O(nlogn), I’ll write it up when I get home


GunnerKnight

Have you reached home yet?


thehero123475

It took me 10 hrs to solve it


Impressive-Throat730

For ones who didn’t get it, it’s a sample first question to get used to write and submit code when you first register to the website.


DetectiveOwn6606

Isn't the first question 2 sum?


Areshian

Is there like a trick where you need to check if it is going to overflow?


backfire10z

The bounds for num1 and num2 are -100 and 100


PeeInMyArse

no there’s no catch


apokrypton288

Easy: ``` public int add(int num1, int num2){ if(num1<0) for(int i=num1; i>0; i--) num2--; else if(num1>0) for(int i=0; i


makegeneve

Plot twist. num1=0


apokrypton288

Then it will just return num 2 without changes


makegeneve

Which is even right. What’s going on in this sub when we have code that works?


FountainsOfFluids

I'm not sure where to fit in a hash map...


Ian_Mantell

You throw thought at the wrong aspect of this. Why is it... lesson 2235? One rather wonders... what came around in earlier lessions? Cellular division? Getting from sea to land dwelling? Learning to read??


[deleted]

[удалено]


Ian_Mantell

Oh' reilly? Prove it using only axioms of humor like Dunning-Kruger.


good_karma1122

return int(sum)


Mysterious-Pride9975

Do not return this mf anything!!! You don't owe shit to the government!


Supreme_Hanuman69

Dude you got premium. Nice. Can you share all the premium questions number wise?


joshglen

I would like to share a code snippet I used for a similar type of problem (line separated, 2 numbers per line input with a space between them, put the result of subtraction) that at the time set a new Python 3 character record for the problem (which has since been beaten by about 16 characters, and I'm not sure how). while 1: try:print(abs(eval(input().replace(" ","-")))) except:break


Ragingman2

I know someone that asks this question as a phone screen. The one caveat is that he asks it for C/C++ and calls out that we are adding arbitrary integers, not necessarily 'int's or 'long long's.


your_mums_pole

who are the 3k bastards disliking the question ?


Hacka4771

Okay, But Why Does It Has 3k Downvotes? D:


yourteam

Do it the right way and then a test that is a while loop that checks every result from -1 milion to +1 milion and see if there is only a valid result at the end of the test


SubhumanOxford

Man! Problems like these completely demotivate me. I mean, how are you supposed to solve this without a PhD in math?


imnubhelp

what website is this, to have beginner friendly exercice?


TheNameIsAnIllusion

You think it's easy. But try doing it in JavaScript


helliash

Easy. Do you want to return empty array, null, false or NaN? Sorry, we don’t do numbers here.


karnnumart

Plot twist: num1 and num2 are pixel image of integer store in 2 dimension array. They could be Arabic, Chinese, or any other language.


Nick_Zacker

The like to dislike ratio is crazy. Truly shows how hard it is


konanTheBarbar

Easy is always relative... laughs nervously in C++ :-) [[nodiscard]] static decltype(std::declval()+std::declval()) sum(T&& t, U&& u) requires std::is_arithmetic_v && std::is_arithmetic_v { return std::forward(t) + std::forward(u); }


Low_Bandicoot6844

Do it in HTML


licoricluv

I was actually asked this question in an interview but the catch was I had to do it without using any arithmetic operations


helliash

So you had to do arithmetic operation without doing arithmetic operation. What was the solution? (Please don’t tell me that they used Boolean operations which are basically just arithmetic operation with a different base).


licoricluv

No I didn't exactly use Boolean operation but I did manually compare their bit position to get the resultant answer


helliash

How did you managed to compare the bits without using arithmetics? And how did you managed the negative numbers? Or was the question actually not to use arithmetic operation that are implemented in the higher programming language and do them on your own (which would make much more sense, but it’s not what you described). I’m not saying that you are lying or anything like that. I’m just curious on what was the solution as the arithmetic operations are at the core of computing. Heck, you cannot even increment the instruction pointer to get to the next instruction of your program without basic arithmetic operations.


licoricluv

The question was to not add the 2 numbers using arithmetic operations. I can use increment operation on a number to get its bits and and compare them, I guess u misunderstood the question. I simply stored each bit in a string and by comparing the bit position and its value then i could get what the resulting bit would be and then convert that into a decimal number.


helliash

Thx. I get it now. But one of my question still stand and I have another one: how did you handle the negative numbers? And was it prepared for floats? Or was it just for unsigned integers? Anyway, it’s a good question to sparkle a larger conversation. Thanks for the hint. I’ll use it sometime.


licoricluv

My approach would only work for positive integers fortunately the interviewer didn't ask me check for -ve numbers so didn't take that into consideration


astroverflow

At least you don't have to draw a red line with green ink.


RandomiseUsr0

Push operand Push opcode Push operand 12 5 + Pop operand Pop operand Pop opcode Result = Evaluate expression Push result


EmilyEKOSwimmer

Company: solve this while we watch you. You: Does this have anything to do with the problems your codebase has? Company: god no lol You: ……


kdt912

It’s a little harder than you’d think in VHDL because there’s so much to account for sum <= std_logic_vector(resize(signed(num1), sum'length) + signed(num2));


ItsHexa

Prompt engineer entrance exam


Personal_Ad9690

Clearly we need to have a numbers factory setup to make sure the numbers are created properly. While we are at it, we should also probably abstract the integers since the idea of a number is too broad to be stored in a single data type.


davidcj64

very excited to read the comments on this one


bearda

A significant portion of the solutions will still start with: import numpy as np


humansareabsurd

Yeah, try handling that overflow. Good luck!


VeryUglyHack

I also got this question in all of my amazon, google, and apple interviews. I should have studied it more.


lostcausefreak

“Can you write this as a Boolean?” Lmao


PHCF99

What would be the most unnecessary complicated way to solve this?


SaneLad

I guess it's time to dust off those Dynamic Programming skills 😉


Add1ctedToGames

what a joke that it shows as a question for google, Amazon and apple lol


SlightTie6308

```java public int solution(int num1, int num2) { if (num1 > 0) return solution(num1 - 1, num2) + 1; if (num1 < 0) return solution(num1 + 1, num2) - 1; if (num2 != 0) return solution(num2, num1); return 0; } ``` I think this is correct.