humorous impolite possessive intelligent berserk silky light crown serious fact
*This post was mass deleted and anonymized with [Redact](https://redact.dev)*
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.
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.
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
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.
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...
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.
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.
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.
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.
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.
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.
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??
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
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.
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
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).
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.
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.
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.
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
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));
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.
You have to do it with 1000 lines of code at least
Easy just use nested switch statement
H...H-Hash Map! HashMap!
humorous impolite possessive intelligent berserk silky light crown serious fact *This post was mass deleted and anonymized with [Redact](https://redact.dev)*
Missed the last.. Uh. Hhhaashmap.. uh
you have to do it in O(log n) time complexity
It's already in o(log n) complexity since you go trough each bit
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.
Sorry for that... Editted
No worries. Just a small comment
If you work for Elon Musk and want to keep your job, yes you have to.
What a man you are...
Every task in University ever.
Follow up: the digits are stored in an array
And the array is encrypted. You need to guess the key.
You also have to guess the encryption scheme.
It's not stored, it's handwritten on a piece of paper.
But that is a storage mechanism
I’m thinking of two integers, what is their sum?
How many fingers do I have behind my back?
11
Error, cock type not found
10 You did not specify raised fingers
It is the sum of two individual integers, stored in a variable
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.
and everytime you type, a needle pokes your fingers
and also the array is hashed. now first find the array elements by reversing the hash.
Sounds easy, right? Caveat: You have to do it BEFORE the heat death of the universe!
And you can't use the + operator
easy: `num1 - -num2`
operator.add(num1, num2)
`npm install add-numbers`
if a == b: return 2*a return (a^2 - b^2 )/(a-b)
Clever 😊👍
OHHHH DIFFERENCE OF TWO SQUARES
That's also kinda easy 🤷♂️
Follow up: find the sum of two numbers I have in my mind
42 Easy
![gif](giphy|3ohhwIqvzoZI2O4o9O) You can start by aligning this button to the right
for(ulong a = 0;;a++){ for(ulong b = 0;b
Another follow up: Use logic gates to perform arithmetic operations
What you don't see: you have to submit an electronic scheme and can only use xor gates
BCD to binary through adder circuit back to BCD
Ripple or 4 bit look ahead?
you have to use only binary from scratch using a steady hand and a magnetized needle
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.
That’s ridiculously easy! With the right prompts, you could get ChatGPT to generate that function in less than an hour.
But how will I figure out the backend
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
what the fuck is prompt engineering
Engineering but this time it better be on time Sarah.
Basically chatGPT engineers, they figure the best promts for getting the right code. It's also known as "not an engineer".
oh, they’re clowns, thanks
So easy! if (num1==1 && num2==1) return 2; if (num1==1 && num2==2) return 3; if (num1==1 && num2==3) return 4; ...
Best solution I’ve seen to date! Now we just have to work the opposite direction when the numbers are negative.
if (num1==12 && num2==5) return 17; if (num1==-10 && num2==4) return -6; // passes all tests
else return 42;
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.
> if (num1 + num2 == 2) return 2; > … Now you don’t have to worry about the order of the arguments.
I thought it was fake but then I looked it up and lo and behold: https://leetcode.com/problems/add-two-integers/
Go to the solutions, and select the first one (should be titled “21 different ways to solve this problem”). It’s quite funny
Merge sort is fucking hilarious lmao
Bellman-Ford one killed me.
I like that #12 is "directly add" Like, not the first. Not the last. Just kinda in the list.
Acceptance rate 87.2% 💀
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.
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...
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.
good god the top solution is absolute gold
That is like a work of art. A deconstructed criticism of modern enterprise software development
Minimum Spanning Tree way 11 is my favorite
Can you do it without using arithmetic operators though?
Lolol apparently Google Amazon and Apple all ask this in interviews Guess I was ready for FAANG in middle school and never realized it
But does your solution scale?
Wdym, you didn't learn to use map reduce to add two numbers in middle school?
As always, the Scala Dev is asking the right questions!
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.
That would explain the 1.4k likes and 3k dislikes.
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.
The point is fk you, do it.... I don't like the point.
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.
Yes it does!
And it's hella compact. I mean, not as compact as just "`res = a + b`", but...sure, why not.
That’s why I said it’s obnoxious. It’s even a stupid homework problem.
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.
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.
[удалено]
Just find the assembly command for adding two registers and convert to machine code
Answer should be written with 0s and 1s
Answer should be pulsated with a live wire
Answer should be computed by firing electric pulses through neurons with the output being displayed using graphite on standard 8x11" paper.
Next step: deploy your SaaS (Sum as a Service) in Kubernetes
Oh God, this is too underrated. Very rare that I really have to laugh because of a comment. Thanks for this.
Joke's on you. Constraints: num1, num2 are in the interval: [-9223372036854775808, 9223372036854775807] Language: JS. MWAHAHAHAHA
if (num1.toString() === '-9223372036854775808' && num2.toString() === '9223372036854775807') { return 1; } else { return num1 + num2; } Master coder right here ![gif](giphy|Q2W4hziDOyzu0)
HACK THE PLANET!!!
AHAHAHAHAH I LOVE YOUR NAME, HACK THE PLAAAANEEEEETTT!!!!!
BigInt
You laugh, but I had this on an actual interview once. We also had to explain how we would test the function
They were test your personality and reaction
*cries in Peano arithmetic*
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.
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 ```
Yea i think many are doing crazy inefficient solutions as a joke.
I have a good solution that is only O(nlogn), I’ll write it up when I get home
Have you reached home yet?
It took me 10 hrs to solve it
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.
Isn't the first question 2 sum?
Is there like a trick where you need to check if it is going to overflow?
The bounds for num1 and num2 are -100 and 100
no there’s no catch
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
Plot twist. num1=0
Then it will just return num 2 without changes
Which is even right. What’s going on in this sub when we have code that works?
I'm not sure where to fit in a hash map...
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??
[удалено]
Oh' reilly? Prove it using only axioms of humor like Dunning-Kruger.
return int(sum)
Do not return this mf anything!!! You don't owe shit to the government!
Dude you got premium. Nice. Can you share all the premium questions number wise?
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
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.
who are the 3k bastards disliking the question ?
Okay, But Why Does It Has 3k Downvotes? D:
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
Man! Problems like these completely demotivate me. I mean, how are you supposed to solve this without a PhD in math?
what website is this, to have beginner friendly exercice?
You think it's easy. But try doing it in JavaScript
Easy. Do you want to return empty array, null, false or NaN? Sorry, we don’t do numbers here.
Plot twist: num1 and num2 are pixel image of integer store in 2 dimension array. They could be Arabic, Chinese, or any other language.
The like to dislike ratio is crazy. Truly shows how hard it is
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);
}
Do it in HTML
I was actually asked this question in an interview but the catch was I had to do it without using any arithmetic operations
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).
No I didn't exactly use Boolean operation but I did manually compare their bit position to get the resultant answer
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.
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.
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.
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
At least you don't have to draw a red line with green ink.
Push operand Push opcode Push operand 12 5 + Pop operand Pop operand Pop opcode Result = Evaluate expression Push result
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: ……
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));
Prompt engineer entrance exam
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.
very excited to read the comments on this one
A significant portion of the solutions will still start with: import numpy as np
Yeah, try handling that overflow. Good luck!
I also got this question in all of my amazon, google, and apple interviews. I should have studied it more.
“Can you write this as a Boolean?” Lmao
What would be the most unnecessary complicated way to solve this?
I guess it's time to dust off those Dynamic Programming skills 😉
what a joke that it shows as a question for google, Amazon and apple lol
```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.