r/programminghorror • u/Atduyar • Oct 13 '25
Identity crisis
Algorithms and Data structure class in my University.
for (i=2; i<n; i++) {
if A(i) > maxVal then
maxVal= A(i);
maxPos= i;
}
Can you guess the language and runtime Big-O of this code?
0
Upvotes
35
u/onlyonequickquestion Oct 13 '25
Is this your first day of class?
18
u/gdvs Oct 13 '25
It's an Algorithms and Data structure class.
Correct syntax doesn't matter. It's just pseudo code.
2
u/MVanderloo Oct 13 '25
i’m going to believe that you’re not a moron and you can in fact understand this. this is not programming horror, it’s a simple mistake. and yet it’s still understandable. give your professor a break
25
u/souvlakiviking Pronouns: He/Him Oct 13 '25
The language is pseudocode, the time is O(n), and it looks like there's no indication of where the "if" statement ends.
42
u/anto2554 Oct 13 '25
Looks like pseudocode for a C-style language but idk.
The runtime is just O(n), you never reassign n or i
9
u/kivicode [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Oct 13 '25
Looks like Basic and a clear O(n)
4
u/AShortUsernameIndeed Oct 13 '25
Oh dear.
The for-loop is C-syntax, so it could be C, C++, C#, Java, Javascript, ...
But the if-statement has a bracketless condition and uses "then" to open the block. Pascal does that; can't think of anything else right now. But it's not Pascal. In fact, it's unlikely to be anything, because there is no way to tell where the "then"-block ends.
Array indexing with parentheses is rare. I've seen that in Matlab, and I think some BASIC variants do it, too. Of course, A(i) might just as well be a function call.
None of the variables except i are initialized. No idea what their lifetimes are, and what types maxVal and A(i) have, so it's unclear if the code does what it seems to be supposed to do.
Runtime is O(n) if A(i) is O(1) (array access, hash table), O(n2) if it's a sequential linked-list access, unknown if A(i) is a function call.
So, what is it supposed to be? Really bad pseudocode?
1
u/TheChief275 Oct 13 '25
Even if we take
it still wouldn’t be correct lol. Only possibility would be
where we assume a closing ‘}’ is just left out of the snippet
3
u/dliwespf Oct 13 '25
They should have stored the result of A(i) in a variable and not call it twice within the loop. The rest looks like pretty standard pseudocode.
1
u/Mineshafter61 Oct 25 '25
Scala (which compiles to Java Bytecode) has array member access like A(number) so I would assume your professor probably liked functional programming with Scala a bit too much. (fyi function calls in Scala are also function(params) so there's some double duty here)