Interview Question
- 2of 2 votes
AnswerPlease have a look at the following pseudo-code. Please note that the syntax may be completely different from any of programming languages you know. More specifically, indentation does not matter in this code, ":=" denotes the substitution, "=" is the equal comparator, and the condition in FOR statement is boundary-inclusive.
- chiranjibparida123 December 17, 2019 in United States
========
Initial state of an array "a":
[[4, 2, 4, 2],
[4, NULL, 4, 2],
[2, NULL, 8, 2],
[16, NULL, 4, NULL]]
========
Main function:
FUNCTION foo()
FOR y := 0 to 3
FOR x := 0 to 3
IF a[x+1][y] != NULL
IF a[x+1][y] = a[x][y]
a[x][y] := a[x][y]*2
a[x+1][y] := NULL
END IF
IF a[x][y] = NULL
a[x][y] := a[x+1][y]
a[x+1][y] := NULL
END IF
END IF
END FOR
END FOR
END FUNCTION
What is the issue with the above code?
How would you fix it?| Report Duplicate | Flag | PURGE
As the index in for loop is boundary inclusive , [x+1] would lead to ArrayIndexOutOfBound ,
- sharathdotc January 31, 2020restrict the loop n-1 at max