4 ms·
If you want to test somebody's ability to think a problem and not just bullshit 100%, why not ask them questions like "Given two integer variables x and y, writ
by ian-g 6y ago
If you want to test somebody's ability to think a problem and not just bullshit 100%, why not ask them questions like "Given two integer variables x and y, write a function to swap their values without using a third variable"?
It's a pretty small problem, but it's still one that needs some thought to get. Also doesn't weed out people who don't know technical details of a red-black binary tree (for example) without necessarily getting rid of people who could quickly pick up what a red-black binary tree is.
- paxys 6y agoThe vast majority of interviews ask coding questions somewhere in between the difficultly level of swapping two integers and red-black trees. I have interviewed at pretty much every mid-large tech company over the years, and have never once been asked anything involving more than array manipulation, linked lists, binary trees, hash tables and BFS/DFS traversal. And this includes senior+ roles at Google, Facebook etc.
- c1ccccc1 6y agoNice puzzle. Snarky python solution: x, y = y, x Legit solution (I think): x = x + y y = x - y x = x - y Actually, using bitwise xor would be even easier: x = x ^ y y = x ^ y x = x ^ y
- a_e_k 6y agoCareful there with that "legit" solution! With extreme values, it can cause overflows and other weird behavior. For example, in Javascript with: x = 2 y = 9007199254740991 then: x = x + y // 9007199254740992 y = x - y // 1 (!!!) x = x - y // 9007199254740991 Or in a somewhat more sane language like C, it'll usually work but may technically invoke undefined behavior: % cat test.c #include <stdio.h> int main() { int x = 2; int y = 2147483647; x = x + y; y = x - y; x = x - y; printf( "%d %d\n", x, y ); } % gcc -fsanitize=undefined -O0 -g -o test test.c && ./test test.c:5:7: runtime error: signed integer overflow: 2 + 2147483647 cannot be represented in type 'int' test.c:6:7: runtime error: signed integer overflow: -2147483647 - 2147483647 cannot be represented in type 'int' test.c:7:7: runtime error: signed integer overflow: -2147483647 - 2 cannot be represented in type 'int' And even in Python you can end up with it silently turning your ints into longs.