MATLAB & GNU Octave Module 4
Logical arrays and control flow interview questions
Interview questions on logical arrays and control flow: warm-ups, then what each harder question tests and how a good answer starts.
Warm-up questions
Short questions with short answers, each answered in full.
-
Fresher
What is logical indexing, and what does x(x < 0) = 0 do?
Answered in the lesson Logical arrays, comparisons and logical indexing
-
Fresher to mid
What is the difference between & and && in MATLAB and Octave?
Answered in the lesson Decisions: if, elseif, else and switch
-
Fresher
What does a for loop iterate over when it is given a matrix?
Answered in the lesson Loops: for, while, break and continue
Questions with model answers
What each question tests, the first step of a good answer and the follow-ups to expect.
-
Fresher to mid
A logger stores 10 million readings from 8 channels in a 10,000,000-by-8 matrix. Negative values and the error code 999 must become NaN, and the report must list how many values each channel lost. Write the vectorised code, estimate the memory it needs, and say how readings that were already NaN affect the counts.
- What it tests
- Building masks on a matrix, counting along a dimension with sum, sizing logical and double arrays in memory, and knowing that NaN fails every comparison.
- First step
- Build one mask for both faults, bad = X < 0 | X == 999, which is 10,000,000-by-8 like X, and count per channel with sum(bad), which adds down each column. The data take 640 MB as doubles, and every mask of that size takes 80 MB.
- Follow-ups
-
- Should the report also count the readings that were already missing?
- Memory is tight: how do you avoid holding several masks of 80 MB at once?
- How would you test the cleaning code on small data?
From the lesson Logical arrays, comparisons and logical indexing
Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.Opening the model answers with your pass…
Model answers open with any MySmartCoPilot pass.
- Pro pass · 1 week₹79$2.99
- 30 days₹179$5.99
- 1 year₹1,699$45
One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.
Passes can’t be bought right now, so the model answers can’t be opened at the moment. If you have a pass, enter your licence key.
It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).
In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher to mid
A colleague filters temperatures with T(0 < T < 40), and the result keeps every value, -5 and 120 included, with no error. Explain what the expression computes, fix it, and say which test cases would have caught the bug before it shipped.
- What it tests
- Left-to-right evaluation of comparisons, combining masks with &, and choosing test values below, at, inside and above both limits, and NaN.
- First step
- Work the expression out in order: 0 < T runs first and gives a mask of 0s and 1s, and that mask is then compared with 40. Every 0 and every 1 is below 40, so every element passes.
- Follow-ups
-
- Should a reading of exactly 0 or 40 be kept?
- Did either program warn about the chained comparison?
- How do you make this kind of bug hard to write again?
From the lesson Logical arrays, comparisons and logical indexing
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher to mid
An input check reads if loads <= 0, error('invalid loads'), end. It let [5 0 7] through, let an empty vector through, and let [5 NaN 7] through. Explain each case, and rewrite the check so that it stops all three.
- What it tests
- How if reads an array condition, any and all, empty input, NaN in comparisons, and writing a validation that states its intent.
- First step
- Work out the condition for [5 0 7]: loads <= 0 is [0 1 0], and if takes its branch only when every element is non-zero, so the error never fires. The check means any, not all.
- Follow-ups
-
- Why does the empty vector pass, and should it?
- How do you make a NaN load fail the check?
- What tests would you keep beside the check?
From the lesson Decisions: if, elseif, else and switch
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
A script that runs without errors in Octave 11.3 stops in MATLAB at if flags && n > 0, where flags is a 1-by-3 logical array. Explain the difference, give the portable fix, and name two other condition or comparison patterns that behave differently in the two programs.
- What it tests
- Scalar operands of && and ||, how Octave treats array and empty operands, & inside conditions, and character arrays against strings created with double quotes.
- First step
- MathWorks requires each operand of && to evaluate to a scalar logical result, and flags has three elements. Octave 11.3 accepted [1 1] && 1, so the problem never showed there. Decide what the script means, all(flags) or any(flags), and write that.
- Follow-ups
-
- What does Octave do with & in an if condition?
- Why does "beam" == "bean" give different results?
- How do you keep shared code from drifting into Octave-only forms?
From the lesson Decisions: if, elseif, else and switch
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher to mid
A Newton solver reports convergence and prints NaN as the root for some starting points. Its loop is while abs(f) > tol && k < maxit, and it reports success whenever k < maxit. Explain the failure, fix the loop and the report, and say what else a robust loop should check.
- What it tests
- NaN in loop conditions, deciding convergence from the error rather than the count, iteration caps, and reporting failure with an error identifier.
- First step
- Start it at x0 = 0 for f(x) = x^2 - 2: the derivative 2x is 0, so the first step gives Inf and the second NaN. abs(NaN) > tol is false, so the loop ends after 2 passes, and k < maxit calls that a success.
- Follow-ups
-
- Is the tolerance on the residual or on the step, and does it matter?
- Should the loop also stop when x stops changing?
- How do you test the failure path?
From the lesson Loops: for, while, break and continue
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher to mid
A deposit model updates a balance every quarter in a for loop, and a colleague asks you to vectorise it. When can you, how do you show that the two versions give the same balances, and which loops should stay loops?
- What it tests
- Recognising recurrences that have an array form, preallocation, checking two implementations against each other with a tolerance, and steps that depend on a decision about the previous value.
- First step
- Write the recurrence: b(q) = b(q - 1) * (1 + r(q)/4). Each balance is P times a running product of factors, so P * cumprod(1 + r/4) gives all of them at once. Compare the two results with a tolerance, not with isequal.
- Follow-ups
-
- A fixed withdrawal is made each quarter once the balance exceeds a threshold. Can it still be vectorised?
- What does preallocation change in the loop version?
- Why compare with a tolerance rather than with isequal?
From the lesson Loops: for, while, break and continue
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
All MATLAB & GNU Octave interview questions · The MATLAB & GNU Octave track