>ask interviewee to write a program that calculates the average of a list of integers
>he fails
holy shit the CS major meme is real
Ask interviewee to write a program that calculates the average of a list of integers
>average(list);
Boom, done.
>average
>doesn't specify mean or median
>doesn't specify geometric, arithmetic, harmonic mean
Boom, rejected.
then dont complain about shit programmers working with you next time
>implying it isn't the job of the candidate to ensure they have a proper understanding of the requirements
would it be:
>use an array to get all the list of integers
>use the sum of array/ the count of whatever is in the array( getting the count with a for loop for the lengthof the array).
can I have job please.
Why?
Overflow
>use an array to get all the list of integers
You're clearly given a list though.
You'd want to get the length of the list, initialize a sum variable with 0, iterate through the list and adding to the sum, and return the sum divided by the length
The question is asked to check if you understand basic logic as well as the issue of integer overflow
It's like saying you'd look for a project on github doing that when asked to implekent fizzbuzz: beside the point
Doesn't consider arithmetic overflow or truncation.
oh. thanks.Just curious.
Given most of Sup Forums can't average two integers, it's understandable they would struggle to average n integers
>implekent
Boom, thrown out of the country
Yeah, the answer should be approximately this, right? Lists don't really exist in C, so I used an array instead but whatever, same idea
#define LIST_LENGTH 50
int average(int someList*) {
int sum = 0;
for (int i = 0; i < LIST_LENGTH; i++) {
sum += someList[i];
}
return sum;
}
int main() {
int someList[LIST_LENGTH] = {...};
average(someList);
}
>implying an interview test program doesn't use all Ints.
>implekent
Pajeet identified.
Oh whoops, and divide by length
return sum / LIST_LENGTH;
what of these answers would be enough?
var avg = average(list) //assuming the language can do that
var avg = list.reduce(function(a, b) { return a + b; }, 0) / list.length
var a = 0;
for (var i = 0; i < list.length; i++) {
a += list[i];
}
var avg = a/i;
int avg_list(int n, int *arr)
{
float result = 0.0f;
unsigned i;
for (i = 0; i < n; i++)
result += (float) arr[i] / n;
return (int) result;
}
Can I have job now?
That's what I am implying and that's why you need to consider these two things in addition to "summating the numbers and dividing by the length".
ok user, please demonstrate how to harmonically integrate the tallywobble with less than 3 integers or you won't get the job
Remember: You have to understand what I just said and can't ask me anything as per your own words
Fired for wasting company time when a much simpler solution exists
Not OP, but if the function is returning the sum, and not the average directly, you shouldn't call it average(). Besides, that still doesn't do what was asked, since you only added everything on the list, it never did the average.
such as..?
Just saw that post, but point still stands, don't call it average if you don't actually average inside of it.
Found the JS cuck
Yeah, I'm aware, I forgot to divide by LIST_LENGTH, as I said in
ARITHMETIC OVERFLOW YA NIGGERS
What's the average of
int list[] = {1, 2, INT_MAX - 2, 1};
then how do you get around integer overflow?
Really? So you want new employees to waste time reinventing the wheel just to show off the fact that they "understand basic logic"?
js can be used in browser, in server, in desktop, everywhere
can your language be used in browser and in desktop?
Yea, never mind, I just realized you actually did the average in the return, my bad.
You should probably not return an integer if you're doing an average though, but I guess that would depend on what is being asked.
Ensure they have a proper understanding by asking questions, you retard
Good answer, thanks.
Yeah cool, I'm aware of overflows, but what can you do about it?
Switching to a larger data type only moves the problem slightly. I'm assuming you're not allowed to use a "bignum"-esque type.
And what could you do if that was c? Check for overflows in the for loop, and then what? Return -1? call exit()?
>I shouldn't have to prove anything
>never mind that most people coming out of CS aren't worth what we're looking to pay
overflow error
overflow error
Don't call us, we'll call you!
>waste one minute reinventing the wheel makes user a crybaby
It lets me throw a lot of candidates away immediately saving a lot of my time
>Yeah cool, I'm aware of overflows, but what can you do about it?
You can avoid them, ya dingus.
>Switching to a larger data type only moves the problem slightly. I'm assuming you're not allowed to use a "bignum"-esque type.
That's only one solution. The obvious solution is to calculate a running mean so you don't risk overflow.
>And what could you do if that was c? Check for overflows in the for loop, and then what? Return -1? call exit()?
See above.
Uncaught ReferenceError: INT_MAX is not defined(…)
Can JS even have overflow errors
Oh fuck of course, you do the dividing inside the loop instead of at the end. Good call
You can encode numbers as dynamic strings, for instance
use a long if you expect your data to get that big.
i just described 99% of use cases
Just store the answer in a double, then.
See
Do you want the return value as an integer?
Do you want integer average, or floating point average that gets flattened back to an integer?
yeah, that's what I mean with bignum, it's stupidly overkill
some can't even invert a binary tree wtf?
int res = 0;
for(int x : list) {
res += x/list.size();
}
return res
gotcha. thanks.
>javascript
this interview is over
>he didn't do it in scheme or haskell
nice pointer nigger
stop using archaic languages with overflow errors everywhere
god gave you rust, javascript and many other languages with sane variables, but you choose to eat shit
int64 sum = 0;
foreach(int i in list)
{
sum += i;
}
int avg = sum / list.Length;
>The obvious solution is to calculate a running mean
I feel like I should have gotten this, then again, I'm studying EE and not CS.
>Average
>integers
u wot m8 is this sum b8
also
>HR
>being useful
oops, should use double, perhaps long, for res. also the calculation in the loop should cast to double/long ofc.
what if i type iüğğööıııı
Overflow happens regardless of what language you use.
try that with a list of 50 integers that all have a value of 1
The code I would write depends on whether the ints are at risk of overflowing or not.
for i in list:
sum += i
return sum/len(i)
how can this cause an overflow?
>how can this cause an overflow
if you need to ask, you're too dumb to program
my job is secure, for now
what if list would be 2 int_max's? sum would be larger than maximum number => overflow
What happens if you neither have enough RAM or swap? Make it run on a distributed system.
And how many hours are you going to waste trying to convince a young hotshot who thinks he knows everything that he doesn't actually know everything? Yeah, good luck with that.
If my interviewer asks me to program a distributed system to average insanely large lists of integers I will kindly ask him to give me the time to find out how to do that, while being paid for my time spent, or look for someone else.
Do a try catch.
I have a red button under my desk which will alert security to have him escorted out
I've seen this happen before.
They basically want to squeeze free work out of you.
just stop working abruptly and tell them that they have to hire you if they want more.
but it's working
~% cat bla.py
import sys
list = [ sys.maxsize, sys.maxsize ]
sum = 0
for i in list:
sum += i
print(sum/len(list))
~% python bla.py
9.223372036854776e+18
user why doesnt your code output anything?
int vars[] = {INT_MAX, INT_MAX, INT_MAX, 1,2};
int main() {
double res = 0;
for (int x : vars) {
res += (double) x / (sizeof(vars)/4);
}
cout
>of integers
nice reading comprehension there bud
>take a random sample of 1 and binomial test it
kys
is python like javascript where integers don't exist and all numbers are 64-bit floats?
>assuming things about tthe distribution of numbers
js has parseInt()
I will fire my interviewer if he asks that
>try catch
“programmers” need not apply
Anyway, this is how I would write it if I had to code defensively against integer overflow
int average(int *a, size_t n)
{
int err = 0;
int avg = 0;
for (int i = 0; i < n; i++) {
int x = a[i] / n;
avg += x;
err += a[i] - x * n;
}
return avg + err / n;
}
>assuming irrealistic things
No, Python uses longs, 64-bit numbers to store numbers, but it has a mechanism in place where if you go over sys.maxint (which is 2^63-1), it will automatically convert into a bignum type number, js actually has a maximum value, python doesn't really
Nope, but Python has long integers that can basically be as big as your system memory allows
>sys.maxsize
> The largest positive integer supported by the platform’s Py_ssize_t type, and thus the maximum size lists, strings, dicts, and many other containers can have.
Easy. I dont know why you all need the length of the list
Pseudo code:
mean = head(list)
for i in list {
mean += i
mean /= 2
}
>The question is asked to check if you understand basic logic as well as the issue of integer overflow
Should have said that then instead of "write a program that calculates the average of a list of integers".
I'm not going to do things in a round-about way unless I am given a reason. If there are prebuilt functions that will do what is required then I am going to use them.
What the fuck is worng with people in this thread? Everything you guys posted is utter shit and pajeet tier.
Also, since when a list is a fucking array?
so what is the probability a random element is a number if the list is not a word?
>Easy. I dont know why you all need the length of the list
Because that code does not give the mean.
sum(x)/len(x)
even average(list)?
And this if I had to code *really, really* defensively against integer overflow
int average(int *a, size_t n)
{
int err = 0;
int avg = 0;
for (int i = 0; i < n; i++) {
int x = a[i] / n;
avg += x;
err += a[i] - x * n;
if (err > INT_MAX / 2) {
int y = err / n;
avg += y;
err -= y * n;
}
}
return avg + err / n;
}
Fails on circularly linked lists
just use rust or python
This ends the interview.
I aint got told shit about linked lists
Lol, you're treating integers as if they're floats or doubles.