Skip to content
  1. Home
  2. Interview Questions
  3. MATLAB
Enterprise & Legacy

100 MATLAB Interview Questions and Answers (2026)

The most asked MATLAB interview questions with structured answers — a quick answer, a clear explanation and working code for each. Covers basics for freshers through advanced, scenario and coding questions for experienced developers.

Updated November 2026 Reviewed by Abhishek Kumar 120 min read
100Questions
11Freshers
9Intermediate
20Experienced

Key takeaways

  • 100 MATLAB interview questions, grouped by level: freshers, intermediate, experienced and coding.
  • Every answer starts with a one-line “Quick answer” you can say out loud, followed by the explanation and runnable code.
  • Use the filter bar to practise only the level you are interviewing for, then test yourself with the free MATLAB quiz.

What is MATLAB, and why do interviewers ask about it?

MATLAB is a high‑performance language for technical computing, seamlessly integrating numerical analysis, matrix operations, and powerful visualisation. This page compiles the most frequently asked MATLAB interview questions – from fundamental syntax and data types to object‑oriented programming, parallel computing, and design patterns – essential for engineers, data scientists, and researchers.

Why companies hire for MATLAB

  • Optimised for matrix and vector operations
  • Rich built‑in functions for mathematics and engineering
  • Powerful visualisation and plotting capabilities
  • Specialised toolboxes for domain‑specific applications
  • Simulink for model‑based design and simulation
  • Widely adopted in academia and industry

Basic MATLAB Interview Questions for Freshers

11 questions

Start here if you are a fresher or switching to MATLAB. These fundamentals are asked in almost every first-round and campus interview.

How interviewers ask it“In simple words, what is MATLAB? Explain it like you would to a new teammate.”

What is MATLAB?

BeginnerMATLAB
Quick answerMATLAB (Matrix Laboratory) is a high-performance language for technical computing. It integrates computation, visualization, and programming in an easy-to-use environment.
  • Matrix-based: Built around matrices and arrays
  • Interactive: Command-line interface and scripts
  • Visualization: Powerful plotting and graphics
  • Toolboxes: Specialized application-specific functions
  • Cross-platform: Windows, macOS, Linux
Matlab
% Hello World in MATLAB
disp('Hello, World!')

How interviewers ask it“Suppose you need to declare variables in MATLAB — how do you approach it, and what can go wrong?”

How to declare variables in MATLAB?

BeginnerMATLAB
Quick answerVariables in MATLAB are dynamically typed. Simply assign a value to a variable name without explicit declaration.
  • Dynamic typing: No type declaration needed
  • Assignments: variable = value
  • whos: Show all variables in workspace
  • clear: Remove variables from workspace
  • Case-sensitive: Variable names are case-sensitive
Matlab
% Variables in MATLAB
mutableVar = 'Hello';  % Mutable variable (dynamic typing)
immutableVar = 'World'; % Variables can be reassigned
inferred = 42;          % Type inference
whos                   % Show all variables

% Display
disp(mutableVar)
disp(immutableVar)
disp(inferred)

How interviewers ask it“Let's start with the basics — how would you explain the data types in MATLAB?”

What are the data types in MATLAB?

BeginnerMATLAB
Quick answerMATLAB has numeric, logical, character, string, cell, structure, and function handle data types.
  • Numeric: double, single, int8, int16, int32, int64, uint8, etc.
  • Logical: true/false
  • Character: 'A'
  • String: "Hello MATLAB"
  • Cell array: {1, 'Hello', 3.14}
  • Structure: person.name = 'Alice'
  • Function handle: @(x) x^2
Matlab
% Data Types in MATLAB
% Numeric types
intNum = 10;            % Double by default
singleNum = single(10); % Single precision
int8Num = int8(10);     % 8-bit integer
uint16Num = uint16(100); % Unsigned 16-bit

% Floating point
floatNum = 3.14;
doubleNum = 3.14159;

% Logical (Boolean)
isActive = true;
isInactive = false;

% Characters and strings
char = 'A';
str = 'Hello MATLAB';

% Arrays
arr = [1, 2, 3, 4, 5];

% Cell arrays
cellArr = {1, 'Hello', 3.14};

% Structures
person.name = 'Alice';
person.age = 25;

% Type checking
isa(intNum, 'double')  % true

How interviewers ask it“How would you define functions? Walk me through your approach.”

How to define functions in MATLAB?

BeginnerMATLAB
Quick answerFunctions in MATLAB are defined using the function keyword. They can have multiple outputs and support anonymous functions.
  • Basic: function result = name(args)
  • Multiple outputs: function [out1, out2] = name(args)
  • Default parameters: nargin check
  • Anonymous functions: @(x) x^2
  • Function handles: @functionName
Matlab
% Functions in MATLAB
% Basic function
function result = add(a, b)
    result = a + b;
end

% Single-expression function
function result = subtract(a, b)
    result = a - b;
end

% Default parameters
function result = greet(name)
    if nargin < 1
        name = 'Guest';
    end
    result = ['Hello, ' name '!'];
end

% Function with multiple outputs
function [quotient, remainder] = divide(a, b)
    quotient = floor(a / b);
    remainder = mod(a, b);
end

% Anonymous function (lambda)
multiply = @(a, b) a * b;

% Higher-order function
function result = operate(a, b, operation)
    result = operation(a, b);
end

% Usage
disp(add(5, 3))
disp(subtract(10, 4))
disp(greet('Alice'))
[q, r] = divide(10, 3);
disp(q)
disp(r)
disp(operate(6, 7, multiply))

How interviewers ask it“Let's start with the basics — how would you explain arrays in MATLAB?”

What are arrays in MATLAB?

BeginnerMATLAB
Quick answerArrays are the fundamental data type in MATLAB. They can be vectors, matrices, or multidimensional.
  • Creation: [1, 2, 3] or 1:3
  • Matrix: [1 2 3; 4 5 6]
  • Access: arr(3) (1-indexed)
  • Modification: arr(3) = 10
  • Operations: length, size, numel
Matlab
% Arrays in MATLAB
% Array creation
numbers = [1, 2, 3, 4, 5];
strings = {'Apple', 'Banana', 'Orange'};
mixed = [1, 2, 3.14];  % Mixed numeric

% Matrix
matrix = [1 2 3; 4 5 6; 7 8 9];

% Access and modify
numbers(3)  % Access element (1-indexed)
numbers(3) = 10;  % Modify element

% Array operations
length(numbers)
size(numbers)
numel(numbers)

% Iteration
for num = numbers
    disp(num)
end

% Array functions
doubled = numbers * 2;  % Element-wise multiplication
filtered = numbers(numbers > 2);
sumVal = sum(numbers);
meanVal = mean(numbers);

% Cell arrays vs arrays
cellArr = {1, 2, 3};
normalArr = [1, 2, 3];

% Display
disp(doubled)
disp(filtered)
disp(sumVal)

How interviewers ask it“In simple words, what are collections? Explain it like you would to a new teammate.”

What are collections in MATLAB?

BeginnerMATLAB
Quick answerMATLAB collections include arrays, cell arrays, structures, and containers.Map for key-value pairs.
  • Numeric arrays: Homogeneous numeric data
  • Cell arrays: Heterogeneous data
  • Structures: Named fields
  • containers.Map: Dictionary-like key-value pairs
  • Operations: filter, map, reduce using array functions
Matlab
% Collections in MATLAB
% Numeric arrays
immutableList = [1, 2, 3, 4, 5];
mutableList = [1, 2, 3];
mutableList(end+1) = 4;  % Append
mutableList(2) = [];     % Remove element

% Cell arrays (can hold different types)
cellList = {1, 'Hello', 3.14};
cellList{end+1} = 'World';

% Structures (maps)
person.name = 'Alice';
person.age = 25;
person.city = 'NYC';

% Structure array
people(1).name = 'Alice';
people(2).name = 'Bob';

% Containers.Map (dictionary)
mapObj = containers.Map();
mapObj('key1') = 'value1';
mapObj('key2') = 'value2';

% Collection operations
numbers = [1, 2, 3, 4, 5, 6];
evens = numbers(mod(numbers, 2) == 0);
doubled = numbers * 2;
sumVal = sum(numbers);
exists = any(numbers > 10);
allEven = all(mod(numbers, 2) == 0);

disp(evens)
disp(doubled)
disp(sumVal)

How interviewers ask it“Let's start with the basics — how would you explain structures in MATLAB?”

What are structures in MATLAB?

BeginnerMATLAB
Quick answerStructures are data containers with named fields, similar to objects in other languages.
  • Creation: person.name = 'Alice'
  • struct function: struct('name', 'Bob', 'age', 30)
  • Access: person.name
  • Arrays of structures: people(1).name = 'Alice'
  • Nested structures: person.address.city = 'NYC'
Matlab
% Structures (Data Classes) in MATLAB
% Define structure
person.name = 'Alice';
person.age = 25;
person.city = 'Unknown';

% Using struct function
person2 = struct('name', 'Bob', 'age', 30, 'city', 'LA');

% Copy
person3 = person;
person3.age = 26;

% Access
disp(person.name)
disp(person.age)

% Class definition (separate file)
% classdef Person
%     properties
%         name
%         age
%         city
%     end
%     methods
%         function obj = Person(name, age, city)
%             obj.name = name;
%             obj.age = age;
%             obj.city = city;
%         end
%     end
% end

How interviewers ask it“Let's start with the basics — how would you explain classes in MATLAB?”

What are classes in MATLAB?

BeginnerMATLAB
Quick answerMATLAB supports object-oriented programming with classes, inheritance, and encapsulation using classdef files.
  • Class definition: classdef MyClass
  • Properties: properties block
  • Methods: methods block
  • Inheritance: classdef Dog < Animal
  • Abstract classes: classdef (Abstract) Shape
Matlab
% Sealed Classes in MATLAB
% Class definition file: Result.m
% classdef (Abstract) Result
% end

% Success.m
% classdef Success < Result
%     properties
%         data
%     end
%     methods
%         function obj = Success(data)
%             obj.data = data;
%         end
%     end
% end

% Error.m
% classdef Error < Result
%     properties
%         message
%     end
%     methods
%         function obj = Error(message)
%             obj.message = message;
%         end
%     end
% end

% Loading.m
% classdef Loading < Result
% end

% Shape classes
% classdef (Abstract) Shape
% end

% Circle.m
% classdef Circle < Shape
%     properties
%         radius
%     end
%     methods
%         function obj = Circle(radius)
%             obj.radius = radius;
%         end
%         function area = getArea(obj)
%             area = pi * obj.radius^2;
%         end
%     end
% end

How interviewers ask it“In simple words, what is null safety? Explain it like you would to a new teammate.”

What is null safety in MATLAB?

BeginnerMATLAB
Quick answerMATLAB uses empty arrays ( []) to represent null values. Checking for emptiness is the primary safety mechanism.

MATLAB uses empty arrays ([]) to represent null values. Checking for emptiness is the primary safety mechanism.

  • Empty arrays: []
  • Check empty: isempty(var)
  • Missing values: missing for tables
  • Try-catch: Error handling
  • Default values: Check and provide defaults
Matlab
% Null Safety in MATLAB
% MATLAB uses empty arrays and missing values

% Empty arrays
emptyArray = [];
nullableString = '';  % Empty string
nonNullableString = 'Hello';

% Check for empty
isempty(emptyArray)  % true

% Missing values (for tables)
% Using ismissing for table data

% Safe access with try-catch
function result = safeLength(arr)
    try
        result = length(arr);
    catch
        result = 0;
    end
end

% Default values
function result = elvis(value, default)
    if isempty(value)
        result = default;
    else
        result = value;
    end
end

% Usage
disp(safeLength([]))
disp(elvis([], 'default'))

% Check for existence
function processString(str)
    if ~isempty(str)
        disp(['String is: ' str])
        disp(['Length: ' num2str(length(str))])
    end
end

How interviewers ask it“In simple words, what are control flow statements? Explain it like you would to a new teammate.”

What are control flow statements in MATLAB?

BeginnerMATLAB
Quick answerMATLAB provides standard control flow: if-else, switch, for, while, and break/continue.
  • If-else: if condition ... else ... end
  • Switch: switch expression ... case ... end
  • For loop: for i = 1:n ... end
  • While loop: while condition ... end
  • Break/Continue: Control loop execution
Matlab
% Control Flow in MATLAB
% If-else
age = 25;
if age < 18
    status = 'Minor';
else
    status = 'Adult';
end
disp(status)

% Switch (switch replacement)
grade = 'A';
switch grade
    case 'A'
        result = 'Excellent';
    case 'B'
        result = 'Good';
    case 'C'
        result = 'Fair';
    otherwise
        result = 'Needs Improvement';
end
disp(result)

% For loop
for i = 1:5
    disp(i)
end

% For loop with step
for i = 1:2:10
    disp(i)
end

% For loop descending
for i = 10:-1:1
    disp(i)
end

% While loop
i = 0;
while i < 5
    disp(i)
    i = i + 1;
end

% Do-while (using while with break)
i = 0;
while true
    disp(i)
    i = i - 1;
    if i <= 0
        break
    end
end

How interviewers ask it“In simple words, what are classes and inheritance? Explain it like you would to a new teammate.”

What are classes and inheritance in MATLAB?

BeginnerMATLAB
Quick answerMATLAB supports inheritance using classdef files. Subclasses inherit properties and methods from base classes.
  • Base class: classdef Animal
  • Inheritance: classdef Dog < Animal
  • Superclass constructor: obj@Animal(args)
  • Method override: function makeSound(obj)
  • Abstract methods: methods (Abstract)
Matlab
% Classes and Inheritance in MATLAB
% Base class file: Animal.m
% classdef Animal
%     properties
%         name
%     end
%     methods
%         function obj = Animal(name)
%             obj.name = name;
%         end
%         function makeSound(obj)
%             disp('Animal sound')
%         end
%     end
% end

% Derived class: Dog.m
% classdef Dog < Animal
%     properties
%         breed
%     end
%     methods
%         function obj = Dog(name, breed)
%             obj@Animal(name);
%             obj.breed = breed;
%         end
%         function makeSound(obj)
%             disp('Woof!')
%         end
%     end
% end

% Abstract class: Vehicle.m
% classdef (Abstract) Vehicle
%     methods (Abstract)
%         start(obj)
%     end
%     methods
%         function stop(obj)
%             disp('Stopped')
%         end
%     end
% end

% Interface simulation
% classdef Flyable
%     methods (Abstract)
%         fly(obj)
%     end
% end

% Duck.m
% classdef Duck < Flyable
%     methods
%         function fly(obj)
%             disp('Flying')
%         end
%         function swim(obj)
%             disp('Swimming')
%         end
%     end
% end

Intermediate MATLAB Interview Questions

9 questions

Questions for candidates with 1–3 years of experience — the concepts interviewers use to check that you have actually built things with MATLAB.

How interviewers ask it“Walk me through properties in MATLAB. Where do they come up in real systems?”

What are properties in MATLAB?

IntermediateMATLAB
Quick answerProperties are class attributes with access control, validation, and dependency features.
  • Properties: properties ... end
  • Access control: properties (Access = private)
  • Dependent properties: properties (Dependent)
  • Validation: set methods
  • Constant properties: properties (Constant)
Matlab
% Properties in MATLAB
% Class with properties
% classdef Person
%     properties
%         name
%         age
%     end
%     properties (Dependent)
%         fullName
%     end
%     properties (Access = private)
%         email
%     end
%     methods
%         function obj = Person(name, age)
%             obj.name = name;
%             obj.age = age;
%         end
%         function value = get.fullName(obj)
%             value = obj.name;
%         end
%         function obj = set.age(obj, value)
%             if value >= 0
%                 obj.age = value;
%             end
%         end
%     end
% end

% Using structure for simple properties
person.name = 'Alice';
person.age = 25;

% Lazy initialization
function value = getExpensiveData()
    persistent cache
    if isempty(cache)
        disp('Computing expensive data...')
        cache = 'Expensive Result';
    end
    value = cache;
end

% Usage
disp(getExpensiveData())
disp(getExpensiveData())

How interviewers ask it“How would you explain static methods to a junior engineer, and what do people most often get wrong?”

What are static methods in MATLAB?

IntermediateMATLAB
Quick answerStatic methods belong to the class, not instances. They are defined using methods (Static).
  • Static methods: methods (Static)
  • Constant properties: properties (Constant)
  • Access: ClassName.method()
  • Factory methods: Create instances
  • Helper functions: Utility functions
Matlab
% Companion Objects in MATLAB
% MATLAB doesn't have companion objects directly
% Using functions in separate files or static methods

% File: MyClass.m
% classdef MyClass
%     properties (Constant)
%         TAG = 'MyClass'
%     end
%     properties (Static)
%         counter = 0
%     end
%     methods (Static)
%         function obj = create()
%             MyClass.counter = MyClass.counter + 1;
%             obj = MyClass();
%         end
%     end
% end

% Using functions in a package
% +myclass/TAG.m
% function value = TAG()
%     value = 'MyClass';
% end

% Usage
% disp(MyClass.TAG)
% MyClass.counter = MyClass.counter + 1;
% obj = MyClass.create();

How interviewers ask it“How would you handle exceptions? Walk me through your approach.”

How to handle exceptions in MATLAB?

IntermediateMATLAB
Quick answerMATLAB uses try-catch-finally blocks for exception handling. Custom errors can be created with error.
  • Try-catch: try ... catch ME ... end
  • Custom errors: error('ID', 'Message')
  • Finally: finally ... end
  • Rethrow: rethrow(ME)
  • Warning: warning('Message')
Matlab
% Exception Handling in MATLAB
% Try-catch block
function result = divide(a, b)
    try
        result = a / b;
    catch ME
        if strcmp(ME.identifier, 'MATLAB:divideByZero')
            disp('Division by zero!')
            result = 0;
        else
            rethrow(ME)
        end
    end
end

% Try as expression
try
    x = 10 / 0;
    result = 'Success';
catch
    result = ['Error: ' ME.message];
end

% Custom exception
function validateAge(age)
    if age < 0 || age > 150
        error('InvalidAgeException:InvalidAge', 'Invalid age: %d', age);
    end
end

% Finally block
function readFile()
    try
        disp('Reading file...')
        % File operations
    catch ME
        disp(['Error reading file: ' ME.message])
    finally
        disp('Closing resources...')
    end
end

% Usage
disp(divide(10, 2))
disp(divide(10, 0))
try
    validateAge(200)
catch ME
    disp(ME.message)
end

How interviewers ask it“Walk me through anonymous functions in MATLAB. Where do they come up in real systems?”

What are anonymous functions in MATLAB?

IntermediateMATLAB
Quick answerAnonymous functions are function handles created at runtime without a separate file. They are defined using @ .

Anonymous functions are function handles created at runtime without a separate file. They are defined using @.

  • Syntax: @(x) x^2
  • Multiple inputs: @(x, y) x + y
  • Function handles: @functionName
  • Higher-order: Pass as arguments
  • Closures: Capture workspace variables
Matlab
% Lambda Expressions in MATLAB
% Basic anonymous function
square = @(x) x^2;

% Anonymous function with multiple inputs
doubled = @(x) x * 2;

% Higher-order functions
function result = performOperation(x, y, operation)
    result = operation(x, y);
end

% Lambda with multiple lines (using function handle)
complexOperation = @(x) (x * 2 + 10);

% Function handle
function result = multiply(x, y)
    result = x * y;
end
multiplyRef = @multiply;

% Returning anonymous function
function op = getOperation(type)
    switch type
        case 'add'
            op = @(a, b) a + b;
        case 'subtract'
            op = @(a, b) a - b;
        otherwise
            op = @(a, b) 0;
    end
end

% Usage
disp(square(5))
disp(performOperation(10, 20, @(x, y) x * y))
add = getOperation('add');
disp(add(5, 3))

How interviewers ask it“How would you explain cell arrays to a junior engineer, and what do people most often get wrong?”

What are cell arrays in MATLAB?

IntermediateMATLAB
Quick answerCell arrays are containers that can hold different data types. They are created using {} .

Cell arrays are containers that can hold different data types. They are created using {}.

  • Creation: {1, 'Hello', 3.14}
  • Access: { } for content, ( ) for cells
  • Operations: cellfun, celldisp
  • Cell array functions: cell2mat, mat2cell
  • Nested cells: {{1, 2}, {3, 4}}
Matlab
% Scope Functions in MATLAB
% Using functions for scope

% let - execute block
function processPerson(person)
    if ~isempty(person)
        name = person.name;
        age = person.age;
        disp(['Name: ' name])
        person.age = 26;
    end
end

% Using cellfun for apply
numbers = {1, 2, 3};
result = cellfun(@(x) x^2, numbers);

% also - perform additional operations
function processList(lst)
    disp(['Before: ' mat2str(lst)])
    lst(end+1) = 4;
    disp(['After: ' mat2str(lst)])
end

% take-if equivalent
function result = takeIf(condition, value)
    if condition(value)
        result = value;
    else
        result = [];
    end
end

% Usage
person = struct('name', 'Alice', 'age', 25);
processPerson(person)
disp(result)
takeIf(@(x) x >= 18, 25)

How interviewers ask it“How would you explain function handles to a junior engineer, and what do people most often get wrong?”

What are function handles in MATLAB?

IntermediateMATLAB
Quick answerFunction handles are variables that reference functions. They allow passing functions as arguments and storing functions.
  • Creation: @functionName
  • Anonymous: @(x) x^2
  • Calling: handle(args)
  • Function functions: feval
  • Arrays of handles: {@sin, @cos, @tan}
Matlab
% Extension Functions in MATLAB
% MATLAB doesn't have extension functions directly
% Using wrapper functions

% String extensions
function result = isEmail(str)
    result = contains(str, '@') && contains(str, '.');
end

function result = addPrefix(str, prefix)
    result = [prefix str];
end

% Numeric extensions
function result = isEven(n)
    result = mod(n, 2) == 0;
end

function result = isOdd(n)
    result = mod(n, 2) ~= 0;
end

% List extensions
function result = secondOrNull(lst)
    if length(lst) >= 2
        result = lst(2);
    else
        result = [];
    end
end

% String word count
function result = wordCount(str)
    words = strsplit(str);
    result = length(words);
end

% Usage
disp(isEmail('test@example.com'))
disp(addPrefix('Hello', 'Greeting: '))
disp(isEven(5))
disp(wordCount('Hello World'))
disp(secondOrNull([1, 2, 3]))

How interviewers ask it“Explain enumerations in MATLAB in detail — what should a new team member understand about them?”

What are enumerations in MATLAB?

IntermediateMATLAB
Quick answerEnumerations are defined using enumeration blocks in classdef files, providing named constants.
  • Enumeration: enumeration ... end
  • Values: RED (1)
  • Properties: Can have associated values
  • Methods: Can have custom methods
  • Usage: Color.RED
Matlab
% Type Aliases in MATLAB
% MATLAB doesn't have type aliases directly
% Using function handles or wrapper functions

% Function alias
add = @(a, b) a + b;
multiply = @(a, b) a * b;

function result = execute(op, a, b)
    result = op(a, b);
end

% For complex types
% Using structures
users = struct();
users.user1 = struct('name', 'Alice', 'age', 25);
users.user2 = struct('name', 'Bob', 'age', 30);

% Using containers.Map
usersMap = containers.Map();
usersMap('user1') = struct('name', 'Alice', 'age', 25);

% Usage
disp(execute(add, 5, 3))
disp(execute(multiply, 5, 3))
disp(users.user1.name)

How interviewers ask it“Walk me through handle classes in MATLAB. Where do they come up in real systems?”

What are handle classes in MATLAB?

IntermediateMATLAB
Quick answerHandle classes are reference types, unlike value classes. They are defined using classdef ... < handle.
  • Reference semantics: Pass by reference
  • Inheritance: classdef MyClass < handle
  • Events: events block
  • Listeners: addlistener
  • Destructor: delete method
Matlab
% Inline Functions in MATLAB
% MATLAB doesn't have inline functions like Kotlin
% Using anonymous functions or function handles

% Regular function
function regularFunction()
    disp('Regular function')
end

% Anonymous function (inlined)
inlineMeasure = @(block) (tic; block(); toc);

% Usage
inlineMeasure(@() pause(0.1));

% Type checking using isa
function result = isType(value, type)
    result = isa(value, type);
end

% Filter by type
function result = filterByType(lst, type)
    result = {};
    for i = 1:length(lst)
        if isa(lst{i}, type)
            result{end+1} = lst{i};
        end
    end
end

mixed = {1, 'Hello', 3.14, 'World'};
strings = filterByType(mixed, 'char');
disp(strings)

How interviewers ask it“Walk me through higher-order functions in MATLAB. Where do they come up in real systems?”

What are higher-order functions in MATLAB?

IntermediateMATLAB
Quick answerHigher-order functions take functions as arguments or return functions. They are implemented using function handles.
  • Parameter: function result = op(a, b, fn)
  • Return: Functions that return handles
  • Composition: compose(f, g)
  • Callbacks: Used in event-driven code
  • Functional programming: Core concept
Matlab
% Higher-Order Functions in MATLAB
% Function that takes a function as parameter
function result = applyOperation(a, b, operation)
    result = operation(a, b);
end

% Function that returns a function
function multiplier = getMultiplier(factor)
    multiplier = @(x) x * factor;
end

% Function composition
function composed = compose(f, g)
    composed = @(x) f(g(x));
end

% Higher-order function with multiple lambdas
function result = processValue(value, transform, filter)
    if filter(value)
        result = transform(value);
    else
        result = [];
    end
end

% Usage
result = applyOperation(10, 20, @(a, b) a + b);
disp(result)

double = getMultiplier(2);
disp(double(5))

square = @(x) x^2;
addTen = @(x) x + 10;
squareThenAddTen = compose(addTen, square);
disp(squareThenAddTen(5))

% Named function
function result = add(a, b)
    result = a + b;
end
disp(applyOperation(10, 20, @add))

Advanced MATLAB Interview Questions for Experienced Developers

20 questions

Senior-level MATLAB questions on internals, performance, design and trade-offs — typical for 3, 5 and 7+ years of experience.

How interviewers ask it“Explain parallel computing in MATLAB in detail — what should a new team member understand about it?”

What is parallel computing in MATLAB?

AdvancedMATLAB
Quick answerMATLAB supports parallel computing through the Parallel Computing Toolbox, using parfor, parfeval, and spmd.
  • parfor: Parallel for loops
  • parfeval: Asynchronous execution
  • parpool: Worker pool management
  • spmd: Single program multiple data
  • GPU computing: gpuArray
Matlab
% Coroutines in MATLAB (using parfor and parallel computing)
% MATLAB doesn't have built-in coroutines like Kotlin
% Using parallel computing toolbox

% Basic parallel execution
function result = fetchData()
    pause(1);  % Simulate network call
    result = 'Data loaded';
end

% Using parfor for parallel loops
function parallelExample()
    results = cell(1, 2);
    parfor i = 1:2
        results{i} = fetchData();
    end
    disp(results)
end

% Using parfeval for async execution
function asyncExample()
    pool = gcp('nocreate');
    if isempty(pool)
        pool = parpool(2);
    end
    f(1) = parfeval(@fetchData, 1);
    f(2) = parfeval(@fetchData, 1);
    results = fetchOutputs(f);
    disp(results)
end

% Timeout
function withTimeout(seconds, fn)
    try
        f = parfeval(fn, 1);
        [~, result] = fetchNext(f, seconds);
        disp(result)
    catch
        disp('Timed out!')
        cancel(f)
    end
end

How interviewers ask it“Walk me through data stores in MATLAB. Where do they come up in real systems?”

What are data stores in MATLAB?

AdvancedMATLAB
Quick answerData stores provide access to large datasets without loading everything into memory. They support big data processing.
  • tall arrays: Work with data larger than memory
  • datastore: Access to large files
  • ImageDatastore: Image collections
  • TabularTextDatastore: Text files
  • Parallel processing: Process data in parallel
Matlab
% Flows in MATLAB (using dataflow and streaming)
% MATLAB doesn't have built-in flows like Kotlin
% Using functions and arrays

% Simple flow
function stream = makeNumberStream(n)
    i = 0;
    stream = @() (i < n && (i = i + 1; true)) && i || [];
end

% Flow operators
function result = streamFilter(stream, pred)
    result = {};
    while true
        val = stream();
        if isempty(val)
            break
        end
        if pred(val)
            result{end+1} = val;
        end
    end
end

function result = streamMap(stream, fn)
    result = {};
    while true
        val = stream();
        if isempty(val)
            break
        end
        result{end+1} = fn(val);
    end
end

% State simulation
state = 0;
function result = getState()
    global state
    result = state;
end
function setState(val)
    global state
    state = val;
end
function increment()
    global state
    state = state + 1;
end

How interviewers ask it“Walk me through timers in MATLAB. Where do they come up in real systems?”

What are timers in MATLAB?

AdvancedMATLAB
Quick answerTimers execute functions at specified intervals, useful for scheduling tasks and asynchronous operations.
  • Timer creation: timer
  • Properties: StartDelay, Period
  • Callback: TimerFcn
  • Methods: start, stop, delete
  • Event handling: ErrorFcn, StopFcn
Matlab
% Channels in MATLAB (using queues and parallel computing)
% MATLAB doesn't have built-in channels like Kotlin
% Using data queues

% Simple queue using cell array
function queue = createQueue()
    queue.items = {};
    queue.lock = false;
end

function queue = enqueue(queue, item)
    while queue.lock
        pause(0.001)
    end
    queue.lock = true;
    queue.items{end+1} = item;
    queue.lock = false;
end

function [queue, item] = dequeue(queue)
    while queue.lock
        pause(0.001)
    end
    queue.lock = true;
    if isempty(queue.items)
        item = [];
    else
        item = queue.items{1};
        queue.items(1) = [];
    end
    queue.lock = false;
end

% Basic channel using parallel computing
function basicChannel()
    queue = createQueue();
    parfeval(@() enqueue(queue, 'Hello'), 0);
    parfeval(@() enqueue(queue, 'World'), 0);
    pause(0.1);
    [~, item1] = dequeue(queue);
    [~, item2] = dequeue(queue);
    disp(item1)
    disp(item2)
end

How interviewers ask it“Explain event listeners in MATLAB in detail — what should a new team member understand about them?”

What are event listeners in MATLAB?

AdvancedMATLAB
Quick answerEvents and listeners enable observer pattern implementation in MATLAB handle classes.
  • Events: events block in handle class
  • Listeners: addlistener
  • Notify: notify to trigger events
  • Callback: Function called on event
  • Properties: event.Property
Matlab
% Sealed Classes and Enum Classes in MATLAB
% Enum class
% classdef Color < int32
%     enumeration
%         RED (1)
%         GREEN (2)
%         BLUE (3)
%     end
% end

% Status enum
% classdef Status
%     enumeration
%         SUCCESS (200)
%         ERROR (500)
%         LOADING (100)
%     end
%     properties
%         code
%     end
%     methods
%         function obj = Status(code)
%             obj.code = code;
%         end
%     end
% end

% Sealed class simulation using abstract classes
% classdef (Abstract) UiState
% end

% Success.m
% classdef Success < UiState
%     properties
%         data
%     end
% end

% Error.m
% classdef Error < UiState
%     properties
%         message
%     end
% end

% Loading.m
% classdef Loading < UiState
% end

% Handling function
function handleState(state)
    if isa(state, 'Success')
        disp(['Data: ' state.data])
    elseif isa(state, 'Error')
        disp(['Error: ' state.message])
    elseif isa(state, 'Loading')
        disp('Loading...')
    end
end

How interviewers ask it“Explain tall arrays in MATLAB in detail — what should a new team member understand about them?”

What are tall arrays in MATLAB?

AdvancedMATLAB
Quick answerTall arrays work with data that doesn't fit in memory. They support delayed execution and parallel processing.
  • Creation: tall(datastore)
  • Operations: Same as regular arrays
  • Delay execution: gather to compute
  • Supported functions: mean, sum, std
  • Visualization: histogram, plot
Matlab
% Generics in MATLAB
% MATLAB is dynamically typed, so generics aren't needed
% Using type checking with isa

% Generic class using cell arrays
function box = createBox(value)
    box.value = value;
end

% Generic function
function [second, first] = swap(first, second)
    % Swap values
    temp = first;
    first = second;
    second = temp;
end

% Generic with constraints
function result = sumNumbers(items)
    result = sum(cell2mat(items));
end

% Type checking function
function processSequence(seq)
    for i = 1:length(seq)
        if isnumeric(seq{i})
            disp(num2str(seq{i}))
        else
            disp(seq{i})
        end
    end
end

% Usage
box = createBox('Hello');
disp(box.value)

[a, b] = swap(1, 2);
disp([a, b])

disp(sumNumbers({1, 2, 3, 4, 5}))

How interviewers ask it“How would you explain GPU arrays to a junior engineer, and what do people most often get wrong?”

What are GPU arrays in MATLAB?

AdvancedMATLAB
Quick answerGPU arrays allow computation on graphics cards for high-performance computing.
  • Creation: gpuArray
  • Operations: Many functions support GPU
  • Data transfer: gather to CPU
  • CUDA kernels: Custom CUDA code
  • Supported functions: arrayfun, bsxfun
Matlab
% Delegation in MATLAB
% Using composition for delegation

% Repository interface
function data = getData(repo)
    data = repo.getData();
end

function saveData(repo, data)
    repo.saveData(data);
end

% Database repository
function repo = createDatabaseRepository()
    repo.getData = @() 'Data from database';
    repo.saveData = @(data) disp(['Saving to database: ' data]);
end

% Cached repository with delegation
function repo = createCachedRepository(databaseRepo)
    cache = [];
    repo.getData = @() getCachedData();
    repo.saveData = @(data) databaseRepo.saveData(data);
    
    function data = getCachedData()
        if isempty(cache)
            data = databaseRepo.getData();
            cache = data;
        else
            data = cache;
        end
    end
end

% Lazy property
function value = getExpensiveValue()
    persistent cache
    if isempty(cache)
        disp('Computing...')
        cache = 'Result';
    end
    value = cache;
end

% Usage
db = createDatabaseRepository();
cached = createCachedRepository(db);
disp(cached.getData())
disp(cached.getData())

How interviewers ask it“Explain the command pattern in MATLAB in detail — what should a new team member understand about it?”

What is the command pattern in MATLAB?

AdvancedMATLAB
Quick answerThe command pattern encapsulates requests as objects, enabling undo/redo functionality.
  • Command object: Encapsulates action
  • Execute method: Perform action
  • Undo method: Reverse action
  • History: Stack of commands
  • Invoker: Executes commands
Matlab
% Object Declarations and Singletons in MATLAB
% Using functions and persistent variables

% Singleton using closure
function config = createAppConfig()
    apiUrl = 'https://api.example.com';
    timeout = 5000;
    
    config.getApiUrl = @() apiUrl;
    config.getTimeout = @() timeout;
    config.printConfig = @() disp(['API URL: ' apiUrl ', Timeout: ' num2str(timeout)]);
end

% Singleton using class
% classdef AppConfig < handle
%     properties (Constant)
%         API_URL = 'https://api.example.com'
%         TIMEOUT = 5000
%     end
%     methods (Static)
%         function printConfig()
%             disp(['API URL: ' AppConfig.API_URL])
%             disp(['Timeout: ' num2str(AppConfig.TIMEOUT)])
%         end
%     end
% end

% Singleton using persistent variable
function config = getAppConfig()
    persistent instance
    if isempty(instance)
        instance = createAppConfig();
    end
    config = instance;
end

% Usage
config = getAppConfig();
disp(config.getApiUrl())
config.printConfig()

How interviewers ask it“Walk me through the singleton pattern in MATLAB. Where does it come up in real systems?”

What is the singleton pattern in MATLAB?

AdvancedMATLAB
Quick answerThe singleton pattern ensures a class has only one instance. Implemented using persistent variables.
  • Persistent variable: Store single instance
  • Instance check: isempty(instance)
  • Initialization: Create on first call
  • Public access: Singleton function
  • Thread-safe: Not by default
Matlab
% DSL (Domain Specific Language) in MATLAB
% Using functions and structures for DSL

% HTML DSL
function html(block)
    fprintf('<html>
')
    block()
    fprintf('</html>
')
end

function body(block)
    fprintf('<body>
')
    block()
    fprintf('</body>
')
end

function h1(text)
    fprintf('<h1>%s</h1>
', text)
end

function p(text)
    fprintf('<p>%s</p>
', text)
end

% Builder pattern
function builder = createUserBuilder()
    builder.name = '';
    builder.age = 0;
    builder.email = '';
    builder.build = @() struct('name', builder.name, ...
                               'age', builder.age, ...
                               'email', builder.email);
end

function user = createUser(varargin)
    builder = createUserBuilder();
    for i = 1:2:length(varargin)
        switch varargin{i}
            case 'name'
                builder.name = varargin{i+1};
            case 'age'
                builder.age = varargin{i+1};
            case 'email'
                builder.email = varargin{i+1};
        end
    end
    user = builder.build();
end

% Usage
html(@() body(@() (h1('Welcome to MATLAB DSL'), ...
                   p('This is a paragraph'), ...
                   p('Another paragraph'))))

user = createUser('name', 'Alice', 'age', 25, 'email', 'alice@example.com');
disp(user)

How interviewers ask it“Explain the factory pattern in MATLAB in detail — what should a new team member understand about it?”

What is the factory pattern in MATLAB?

AdvancedMATLAB
Quick answerThe factory pattern creates objects without specifying the exact class. Implemented using switch statements.
  • Factory function: Creates objects
  • Type parameter: Specifies which class
  • Return: Instance of requested class
  • Decoupling: Client doesn't know concrete classes
  • Extensible: Add new types easily
Matlab
% Annotations in MATLAB
% MATLAB doesn't have built-in annotations like Kotlin
% Using comments and meta-data

% Custom annotation using comments
% @MyAnnotation("test")
function annotatedClass()
    % This is an annotated class
end

% Using function attributes
function annotatedMethod()
    % @MyAnnotation("method")
    disp('Annotated method')
end

% Metadata using struct
function obj = createAnnotated(value)
    obj.value = value;
    obj.annotations = {'MyAnnotation'};
end

% Reading annotations
function readAnnotations(func)
    lines = split(string(fileread([func '.m'])), newline);
    for i = 1:length(lines)
        line = strtrim(lines{i});
        if startsWith(line, '% @')
            disp(['Annotation: ' line(3:end)])
        end
    end
end

% Example usage
annotatedMethod()
% readAnnotations('annotatedMethod')

How interviewers ask it“How would you explain the strategy pattern to a junior engineer, and what do people most often get wrong?”

What is the strategy pattern in MATLAB?

AdvancedMATLAB
Quick answerThe strategy pattern defines a family of algorithms and makes them interchangeable. Implemented using function handles.
  • Strategy interface: Function handle
  • Context: Uses strategy
  • Concrete strategies: Different algorithms
  • Runtime switching: Change behavior dynamically
  • Decoupling: Algorithm independent of client
Matlab
% Reflection in MATLAB
% Using functions for reflection

% Class for reflection examples
person.name = 'Alice';
person.age = 25;
person.city = 'Unknown';

% Basic reflection
function basicReflection()
    person = struct('name', 'Alice', 'age', 25);
    fields = fieldnames(person);
    disp(['Class: struct'])
    disp(['Fields: ' strjoin(fields, ', ')])
end

% Accessing properties
function accessProperties()
    person = struct('name', 'Alice', 'age', 25);
    fields = fieldnames(person);
    for i = 1:length(fields)
        disp([fields{i} ' = ' num2str(person.(fields{i}))])
    end
end

% Calling functions dynamically
function callFunctions()
    person = struct('name', 'Alice', 'age', 25);
    func = @greet;
    result = func(person);
    disp(result)
end

function result = greet(person)
    result = ['Hello, my name is ' person.name];
end

% Create instance
function createInstance()
    person = struct('name', 'Bob', 'age', 30, 'city', 'NYC');
    disp(person)
end

How interviewers ask it“How would you explain the observer pattern to a junior engineer, and what do people most often get wrong?”

What is the observer pattern in MATLAB?

AdvancedMATLAB
Quick answerThe observer pattern defines a one-to-many dependency between objects. Implemented using events and listeners.
  • Subject: Maintains observers
  • Observer: Receives updates
  • Events: events block
  • Listeners: addlistener
  • Notification: notify
Matlab
% Coroutine Context and Dispatchers in MATLAB
% Using parallel computing toolbox

% Different dispatchers
function dispatcherExample()
    % Default - use parfor
    parfor i = 1:3
        disp(['Worker ' num2str(i) ': ' getenv('COMPUTERNAME')])
    end
    
    % Custom context
    try
        pool = gcp('nocreate');
        if isempty(pool)
            pool = parpool(2);
        end
        disp(['Number of workers: ' num2str(pool.NumWorkers)])
    catch
        disp('Parallel computing toolbox not available')
    end
end

% ThreadLocal simulation
function threadLocalExample()
    % Use parfor for thread-local data
    results = cell(1, 3);
    parfor i = 1:3
        results{i} = ['Thread ' num2str(i) ': ' getenv('COMPUTERNAME')];
    end
    disp(results)
end

% Supervisor job
function supervisorExample()
    try
        parfor i = 1:2
            if i == 1
                error('Error in task 1')
            else
                disp('Task 2 still running')
            end
        end
    catch ME
        disp(['Caught: ' ME.message])
    end
end

How interviewers ask it“Walk me through the decorator pattern in MATLAB. Where does it come up in real systems?”

What is the decorator pattern in MATLAB?

AdvancedMATLAB
Quick answerThe decorator pattern adds behavior to objects dynamically. Implemented using wrapper functions.
  • Component: Base object
  • Decorator: Wraps component
  • Modification: Enhance behavior
  • Chaining: Multiple decorators
  • Flexibility: Add features at runtime
Matlab
% Shared Mutable State in Coroutines (MATLAB)
% Using parallel computing with caution

% Counter with mutex (using parallel pool)
function counterExample()
    counter = 0;
    lock = false;
    
    function increment()
        while lock
            pause(0.001)
        end
        lock = true;
        counter = counter + 1;
        lock = false;
    end
    
    function value = getValue()
        value = counter;
    end
    
    % Simulate parallel access
    try
        parfor i = 1:100
            increment()
        end
    catch
        disp('Parallel execution not available')
    end
    disp(['Final count: ' num2str(getValue())])
end

% Using atomic operations
function atomicCounterExample()
    counter = 0;
    for i = 1:1000
        counter = counter + 1;
    end
    disp(['Atomic count: ' num2str(counter)])
end

% Single-threaded
function singleThreadExample()
    counter = 0;
    for i = 1:1000
        counter = counter + 1;
    end
    disp(['Single thread count: ' num2str(counter)])
end

How interviewers ask it“Walk me through the builder pattern in MATLAB. Where does it come up in real systems?”

What is the builder pattern in MATLAB?

AdvancedMATLAB
Quick answerThe builder pattern constructs complex objects step by step. Implemented using methods that return the builder.
  • Builder: Constructs parts
  • Director: Orchestrates construction
  • Product: Constructed object
  • Fluent interface: Method chaining
  • Validation: Check before building
Matlab
% Flow Operators and Transformations in MATLAB
% Using array operations

% Basic flow transformation
function flowTransformExample()
    numbers = 1:10;
    evens = numbers(mod(numbers, 2) == 0);
    mapped = arrayfun(@(x) ['Number ' num2str(x)], evens, 'UniformOutput', false);
    disp(mapped)
end

% Flow with buffer
function flowBufferExample()
    for i = 1:5
        disp(i)
        pause(0.1)
    end
end

% Flow with conflate
function flowConflateExample()
    for i = 1:10
        disp(i)
        pause(0.05)
    end
end

% Flow with collect latest
function flowCollectLatestExample()
    for i = 1:10
        disp(['Processing ' num2str(i)])
        pause(0.1)
        disp(['Done ' num2str(i)])
    end
end

% FlatMap
function flowFlatMapExample()
    for i = 1:3
        for letter = 'ab'
            disp([num2str(i) '-' letter])
        end
    end
end

How interviewers ask it“Explain the adapter pattern in MATLAB in detail — what should a new team member understand about it?”

What is the adapter pattern in MATLAB?

AdvancedMATLAB
Quick answerThe adapter pattern converts one interface to another. Implemented using wrapper functions.
  • Target: Expected interface
  • Adaptee: Existing interface
  • Adapter: Bridges interfaces
  • Compatibility: Makes incompatible classes work
  • Reusability: Use existing code
Matlab
% Coroutine Scopes and Lifecycle in MATLAB
% Using parallel computing with lifecycle management

% Custom scope
function scope = createScope()
    scope.cancelled = false;
    scope.tasks = {};
end

function scope = launchTask(scope, fn)
    scope.tasks{end+1} = parfeval(fn, 1);
end

function cancelScope(scope)
    scope.cancelled = true;
    for i = 1:length(scope.tasks)
        cancel(scope.tasks{i})
    end
end

% Lifecycle-aware scope
function lifecycleExample()
    scope = createScope();
    launchTask(scope, @() (pause(1); disp('Task 1')));
    launchTask(scope, @() (pause(2); disp('Task 2')));
    pause(0.5);
    cancelScope(scope);
end

% Global scope
function globalScopeExample()
    parfeval(@() (pause(1); disp('GlobalScope')), 1);
    pause(2);
end

% Scope with timeout
function timeoutScope()
    try
        f = parfeval(@() (pause(2); 'Success'), 1);
        [~, result] = fetchNext(f, 1);
        if isempty(result)
            disp('Timed out')
        else
            disp(result)
        end
    catch
        disp('Timed out')
    end
end

How interviewers ask it“Walk me through the facade pattern in MATLAB. Where does it come up in real systems?”

What is the facade pattern in MATLAB?

AdvancedMATLAB
Quick answerThe facade pattern provides a simplified interface to a complex subsystem. Implemented as a wrapper function.
  • Facade: Simplified interface
  • Subsystem: Complex components
  • Simplification: Hide complexity
  • Decoupling: Client doesn't need subsystem details
  • Usage: Library APIs
Matlab
% SharedFlow and StateFlow in MATLAB
% Using observers and state management

% StateFlow simulation
function flow = createStateFlow(initialValue)
    flow.value = initialValue;
    flow.observers = {};
end

function updateState(flow, newValue)
    flow.value = newValue;
    for i = 1:length(flow.observers)
        flow.observers{i}(newValue);
    end
end

function observeState(flow, callback)
    flow.observers{end+1} = callback;
end

% SharedFlow simulation
function flow = createSharedFlow(replay)
    flow.events = {};
    flow.replay = replay;
    flow.replayed = {};
    flow.observers = {};
end

function emitEvent(flow, event)
    flow.events{end+1} = event;
    flow.replayed{end+1} = event;
    if length(flow.replayed) > flow.replay
        flow.replayed(1) = [];
    end
    for i = 1:length(flow.observers)
        flow.observers{i}(event);
    end
end

% Distinct until changed
function distinctExample(flow)
    lastValue = [];
    observeState(flow, @(value) (isempty(lastValue) || ~isequal(value, lastValue)) && ...
                                 (lastValue = value; disp(value)));
end

% Combine flows
function result = combineFlows(flow1, flow2)
    result = createStateFlow(0);
    observeState(flow1, @(value) updateState(result, value + flow2.value));
    observeState(flow2, @(value) updateState(result, flow1.value + value));
end

How interviewers ask it“Walk me through the composite pattern in MATLAB. Where does it come up in real systems?”

What is the composite pattern in MATLAB?

AdvancedMATLAB
Quick answerThe composite pattern treats individual objects and compositions uniformly. Implemented using nested structures.
  • Component: Interface for all
  • Leaf: Individual object
  • Composite: Container of children
  • Operations: Work on both leaf and composite
  • Tree structure: Nested hierarchies
Matlab
% Coroutine Exception Handling in MATLAB
% Using try-catch with parallel computing

% Try-catch in parallel
function tryCatchExample()
    try
        parfeval(@() error('Error'), 1);
    catch ME
        disp(['Caught: ' ME.message])
    end
end

% Exception handler
function exceptionHandler()
    try
        parfeval(@() error('Test'), 1);
        pause(0.1);
    catch ME
        disp(['Handler caught: ' ME.message])
    end
end

% Supervisor for child isolation
function supervisorExample2()
    try
        parfor i = 1:2
            if i == 1
                error('Child 1 error')
            else
                pause(0.2)
                disp('Child 2 still running')
            end
        end
    catch ME
        disp(['Caught: ' ME.message])
    end
end

% Flow exception handling
function flowException()
    try
        disp('1')
        error('Flow error')
    catch ME
        disp(['Flow caught: ' ME.message])
        disp('-1')
    end
end

% Supervisor scope
function supervisorScopeExample()
    try
        parfor i = 1:2
            if i == 1
                error('Error')
            else
                pause(0.1)
                disp('Still running')
            end
        end
    catch
        % Continue execution
    end
end

How interviewers ask it“Walk me through the visitor pattern in MATLAB. Where does it come up in real systems?”

What is the visitor pattern in MATLAB?

AdvancedMATLAB
Quick answerThe visitor pattern separates algorithms from object structures. Implemented using function handles.
  • Visitor: Defines operations
  • Element: Accepts visitors
  • Double dispatch: Determines which operation
  • Extensibility: Add operations without modifying elements
  • Separation: Algorithm and structure separate
Matlab
% Channel Producers and Consumer Patterns in MATLAB
% Using queues for producer-consumer patterns

% Producer-Consumer pattern
function producerConsumer()
    queue = {};
    
    % Producer
    parfeval(@() (for i = 1:20; queue{end+1} = i; disp(['Produced: ' num2str(i)]); pause(0.1); end), 1);
    
    % Consumer
    parfeval(@() (for i = 1:20; if ~isempty(queue); val = queue{1}; queue(1) = []; disp(['Consumed: ' num2str(val)]); end; pause(0.15); end), 1);
end

% Fan-out pattern
function fanOutExample()
    queue = {};
    
    % Producer
    parfeval(@() (for i = 1:20; queue{end+1} = i; end), 1);
    
    % Multiple consumers
    for id = 1:3
        parfeval(@() (for i = 1:20; if ~isempty(queue); val = queue{1}; queue(1) = []; disp(['Consumer ' num2str(id) ': ' num2str(val)]); end; pause(0.1); end), 1);
    end
end

% Fan-in pattern
function fanInExample()
    queue = {};
    
    % Multiple producers
    for id = 1:3
        parfeval(@() (for i = 1:5; queue{end+1} = ['Producer ' num2str(id) ': ' num2str(i)]; pause(0.05); end), 1);
    end
    
    % Single consumer
    parfeval(@() (for i = 1:15; if ~isempty(queue); val = queue{1}; queue(1) = []; disp(val); end; end), 1);
end

How interviewers ask it“Explain the proxy pattern in MATLAB in detail — what should a new team member understand about it?”

What is the proxy pattern in MATLAB?

AdvancedMATLAB
Quick answerThe proxy pattern controls access to another object. Implemented using wrapper functions.
  • Subject: Real object
  • Proxy: Controls access
  • Access control: Check permissions
  • Lazy loading: Create on demand
  • Logging: Log access
Matlab
% Coroutine Cancellation in MATLAB
% Using cancellation with parallel computing

% Cooperative cancellation
function cooperativeCancellation()
    cancelled = false;
    
    parfeval(@() (for i = 1:100; if cancelled; break; end; disp(['Working: ' num2str(i)]); pause(0.05); end), 1);
    pause(0.2);
    cancelled = true;
end

% Cancellation with finally
function cancellationFinally()
    try
        parfeval(@() (for i = 1:100; disp(['Processing: ' num2str(i)]); pause(0.1); end), 1);
        pause(0.25);
        disp('Cleaning up')
        pause(0.1)
        disp('Cleanup done')
    catch
        disp('Cleaning up')
    end
end

% Cancellation with timeout
function cancellationTimeout()
    try
        f = parfeval(@() (for i = 1:10; pause(0.2); disp(['Iteration: ' num2str(i)]); end), 1);
        [~, result] = fetchNext(f, 1);
        if isempty(result)
            cancel(f)
            disp('Timed out')
        end
    catch
        disp('Timed out')
    end
end

% Custom cancellation check
function customCancellation()
    cancelled = false;
    
    parfeval(@() (i = 0; while ~cancelled && i < 1000; if mod(i, 100) == 0; disp(['Still running: ' num2str(i)]); end; i = i + 1; pause(0.001); end), 1);
    pause(0.1);
    cancelled = true;
end

How interviewers ask it“Explain the chain of responsibility in MATLAB in detail — what should a new team member understand about it?”

What is the chain of responsibility in MATLAB?

AdvancedMATLAB
Quick answerThe chain of responsibility passes requests along a chain of handlers. Implemented using linked structure.
  • Handler: Processes or forwards
  • Chain: Linked list of handlers
  • Processing: Each handler decides
  • Decoupling: Sender doesn't know which handler
  • Flexibility: Add/remove handlers
Matlab
% Testing Coroutines in MATLAB
% Using unit testing framework

% Basic test
function testCoroutine()
    result = [];
    
    f = parfeval(@() (pause(1); 'Success'), 1);
    [~, result] = fetchNext(f);
    assert(strcmp(result, 'Success'), 'Test failed');
end

% Test with delay
function testWithDelay()
    result = [];
    
    f = parfeval(@() (pause(1); 'Done'), 1);
    pause(1);
    [~, result] = fetchNext(f);
    assert(strcmp(result, 'Done'), 'Test failed');
end

% Test multiple coroutines
function testMultipleCoroutines()
    results = {};
    
    f1 = parfeval(@() (pause(0.5); 'Task 1'), 1);
    f2 = parfeval(@() (pause(0.3); 'Task 2'), 1);
    
    [~, r1] = fetchNext(f1);
    [~, r2] = fetchNext(f2);
    results = {r1, r2};
    assert(isequal(results, {'Task 1', 'Task 2'}), 'Test failed');
end

% Time control test
function timeControlTest()
    counter = 0;
    
    f = parfeval(@() (for i = 1:5; pause(1); counter = counter + 1; end), 1, counter);
    pause(3);
    cancel(f);
    assert(counter <= 4, 'Test failed');
end

How interviewers ask it“How would you explain the memento pattern to a junior engineer, and what do people most often get wrong?”

What is the memento pattern in MATLAB?

AdvancedMATLAB
Quick answerThe memento pattern captures and restores object state. Implemented using structures.
  • Memento: Stores state
  • Originator: Creates/restores mementos
  • Caretaker: Manages mementos
  • Undo/Redo: Restore previous states
  • Encapsulation: State is external
Matlab
% MATLAB Multiplatform
% MATLAB runs on multiple platforms with platform-specific features

% Platform-specific code
function name = platformName()
    if ispc
        name = 'Windows';
    elseif ismac
        name = 'macOS';
    elseif isunix
        name = 'Linux';
    else
        name = 'Unknown';
    end
end

function greet()
    disp(['Hello from ' platformName()])
end

% Platform-specific class
function version = getVersion()
    version = ver('MATLAB');
    disp(['MATLAB version: ' version(1).Version])
end

% Platform info
function getInfo()
    disp([greet() ' version ' getVersion()])
end

% Serialization
function str = encodeUser(user)
    str = sprintf('%d
%s
%s', user.id, user.name, user.email);
end

function user = decodeUser(str)
    lines = strsplit(str, '
');
    user = struct('id', str2double(lines{1}), ...
                  'name', lines{2}, ...
                  'email', lines{3});
end

MATLAB Coding Interview Questions (Programs with Code)

60 questions

Hands-on MATLAB programs that are asked in coding rounds and machine tests. Read the code, then try writing it yourself without looking.

How interviewers ask it“Live coding: Reverse a string in MATLAB. Talk me through your approach first.”

How do you reverse a string in MATLAB?

CodingMATLAB
Quick answerReverse a string using fliplr or manual iteration.
  • Built-in: fliplr(str)
  • Manual: Loop from end to start
  • Complexity: O(n) time
Matlab
% Reverse a string
function result = reverseString(str)
    result = fliplr(str);
end
disp(reverseString('hello'))  % "olleh"

% Manual implementation
function result = reverseStringManual(str)
    result = '';
    for i = length(str):-1:1
        result = [result str(i)];
    end
end

How interviewers ask it“Can you check palindrome in MATLAB? Then walk me through the edge cases you would test.”

How do you check palindrome in MATLAB?

CodingMATLAB
Quick answerCheck if a string is a palindrome using fliplr or two-pointer approach.
  • Method: strcmp(cleaned, fliplr(cleaned))
  • Two-pointer: Compare from both ends
  • Case insensitive: lower
  • Ignore non-alphanumeric: isstrprop
Matlab
% Check palindrome
function result = isPalindrome(str)
    cleaned = lower(str(isstrprop(str, 'alphanum')));
    result = strcmp(cleaned, fliplr(cleaned));
end
disp(isPalindrome('racecar'))  % true
disp(isPalindrome('hello'))   % false

% Two-pointer approach
function result = isPalindromeTwoPointer(str)
    cleaned = lower(str(isstrprop(str, 'alphanum')));
    left = 1;
    right = length(cleaned);
    result = true;
    while left < right
        if cleaned(left) ~= cleaned(right)
            result = false;
            return;
        end
        left = left + 1;
        right = right - 1;
    end
end

How interviewers ask it“Can you find max in array in MATLAB? Then walk me through the edge cases you would test.”

How do you find max in array?

CodingMATLAB
Quick answerFind maximum using max or manual iteration.
  • Built-in: max(arr)
  • Manual: Iterate and track max
  • Empty array: Returns []
Matlab
% Find max in array
function result = findMax(arr)
    result = max(arr);
end
disp(findMax([1, 5, 3, 9, 2]))  % 9

% Manual implementation
function result = findMaxManual(arr)
    result = arr(1);
    for i = 2:length(arr)
        if arr(i) > result
            result = arr(i);
        end
    end
end

How interviewers ask it“Coding-round task — remove duplicates in MATLAB. Explain your approach before you write the code.”

How do you remove duplicates in MATLAB?

CodingMATLAB
Quick answerRemove duplicates using unique.
  • Built-in: unique(arr)
  • Preserve order: unique(arr, 'stable')
  • Complexity: O(n log n)
Matlab
% Remove duplicates
function result = removeDuplicates(arr)
    result = unique(arr);
end
disp(removeDuplicates([1, 2, 2, 3, 3, 4]))  % [1, 2, 3, 4]

% Using set
function result = removeDuplicatesSet(arr)
    result = unique(arr);
end

How interviewers ask it“Can you merge arrays in MATLAB? Then walk me through the edge cases you would test.”

How do you merge arrays in MATLAB?

CodingMATLAB
Quick answerMerge arrays using concatenation.
  • Method: [arr1, arr2] or horzcat
  • Vertical: [arr1; arr2]
  • Unique: union
Matlab
% Merge arrays
function result = mergeArrays(arr1, arr2)
    result = [arr1, arr2];
end
disp(mergeArrays([1, 2], [3, 4]))  % [1, 2, 3, 4]

% Alternative
function result = mergeArraysPlus(arr1, arr2)
    result = horzcat(arr1, arr2);
end

How interviewers ask it“Live coding: Convert string to number in MATLAB. Talk me through your approach first.”

How do you convert string to number in MATLAB?

CodingMATLAB
Quick answerConvert using str2double.
  • toNumber: str2double(str)
  • Safe: Check isnan
  • Error handling: Returns NaN on failure
Matlab
% Convert string to number
function result = stringToNumber(str)
    result = str2double(str);
end
disp(stringToNumber('42'))  % 42

% Safe conversion
function result = stringToNumberSafe(str)
    result = str2double(str);
    if isnan(result)
        result = [];
    end
end

How interviewers ask it“Live coding: Loop through map in MATLAB. Talk me through your approach first.”

How do you loop through map in MATLAB?

CodingMATLAB
Quick answerIterate through structure using fieldnames.
  • Method: fieldnames and loop
  • Alternative: for loop with dynamic field names
  • Keys: fieldnames
  • Values: struct2cell
Matlab
% Loop through map (structure)
function loopMap(map)
    fields = fieldnames(map);
    for i = 1:length(fields)
        disp([fields{i} ' => ' num2str(map.(fields{i}))])
    end
end

% Alternative
function loopMapAlternate(map)
    fn = fieldnames(map);
    for i = 1:numel(fn)
        disp([fn{i} ' => ' num2str(map.(fn{i}))])
    end
end

data = struct('name', 'Alice', 'age', 25, 'city', 'NYC');
loopMap(data)

How interviewers ask it“Show me delay function execution in MATLAB — write it out and explain each step as you go.”

Explain delay function execution in MATLAB with an example.

CodingMATLAB
Quick answerDelay using pause or timers.
  • Pause: pause(seconds)
  • Timer: timer object
  • Async: timer with callback
Matlab
% Delay function execution
function delayedExecution(delayMs, block)
    pause(delayMs / 1000);
    block();
end

% Example usage
delayedExecution(2000, @() disp('After 2 seconds'));

% Using timer
function delayedExecutionTimer(delayMs, block)
    t = timer('StartDelay', delayMs / 1000, 'TimerFcn', block);
    start(t);
end

How interviewers ask it“Show me HTTP GET request in MATLAB — write it out and explain each step as you go.”

Explain HTTP GET request in MATLAB with an example.

CodingMATLAB
Quick answerMake HTTP GET using webread or urlread.
  • webread: webread(url)
  • Options: weboptions
  • Error handling: try-catch
Matlab
% HTTP GET request
function data = fetchData(url)
    try
        options = weboptions('Timeout', 10);
        data = webread(url, options);
    catch ME
        disp(['Error: ' ME.message])
        data = [];
    end
end

% Using urlread (older versions)
function data = fetchDataOld(url)
    try
        data = urlread(url);
    catch ME
        disp(['Error: ' ME.message])
        data = [];
    end
end

% Example
% data = fetchData('https://api.example.com/data');

How interviewers ask it“Can you create a promise-like Deferred in MATLAB? Then walk me through the edge cases you would test.”

How do you create a promise-like Deferred in MATLAB?

CodingMATLAB
Quick answerCreate a Deferred using structures and polling.
  • Structure: State and result
  • Polling: Check done flag
  • Error handling: Store error
Matlab
% Create a promise-like Deferred
function deferred = createDeferred(shouldResolve)
    deferred.result = [];
    deferred.done = false;
    
    try
        if shouldResolve
            pause(1);
            deferred.result = 'Success!';
        else
            error('Failed!');
        end
    catch ME
        deferred.error = ME;
    end
    deferred.done = true;
end

% Usage
deferred = createDeferred(true);
while ~deferred.done
    pause(0.1);
end
if isfield(deferred, 'error')
    disp(['Caught: ' deferred.error.message])
else
    disp(deferred.result)
end

How interviewers ask it“Show me factorial in MATLAB — write it out and explain each step as you go.”

Explain factorial in MATLAB with an example.

CodingMATLAB
Quick answerCalculate factorial using recursion or iteration.
  • Recursive: if n <= 1, 1 else n * factorial(n-1)
  • Iterative: prod(1:n)
  • Edge cases: 0! = 1
Matlab
% Factorial
function result = factorial(n)
    if n <= 1
        result = 1;
    else
        result = n * factorial(n - 1);
    end
end
disp(factorial(5))  % 120

% Iterative version
function result = factorialIterative(n)
    result = 1;
    for i = 2:n
        result = result * i;
    end
end

How interviewers ask it“Show me fibonacci in MATLAB — write it out and explain each step as you go.”

Explain fibonacci in MATLAB with an example.

CodingMATLAB
Quick answerCalculate Fibonacci using recursion, iteration, or memoization.
  • Recursive: if n <= 1, n else fib(n-1) + fib(n-2)
  • Iterative: Loop with variables
  • Memoization: containers.Map
Matlab
% Fibonacci
function result = fibonacci(n)
    if n <= 1
        result = n;
    else
        result = fibonacci(n - 1) + fibonacci(n - 2);
    end
end
disp(fibonacci(8))  % 21

% Iterative version
function result = fibonacciIterative(n)
    if n <= 1
        result = n;
        return;
    end
    a = 0;
    b = 1;
    for i = 2:n
        temp = a + b;
        a = b;
        b = temp;
    end
    result = b;
end

How interviewers ask it“Show me fizzBuzz in MATLAB — write it out and explain each step as you go.”

Explain fizzBuzz in MATLAB with an example.

CodingMATLAB
Quick answerFizzBuzz using if-else with modulo operations.
  • Modulo: mod function
  • Order: Check 15 first
  • Range: for loop
Matlab
% FizzBuzz
function fizzBuzz(n)
    for i = 1:n
        if mod(i, 15) == 0
            disp('FizzBuzz')
        elseif mod(i, 3) == 0
            disp('Fizz')
        elseif mod(i, 5) == 0
            disp('Buzz')
        else
            disp(i)
        end
    end
end
fizzBuzz(15)

How interviewers ask it“Can you find missing number in MATLAB? Then walk me through the edge cases you would test.”

How do you find missing number in MATLAB?

CodingMATLAB
Quick answerFind missing number using formula or XOR.
  • Formula: total - sum
  • XOR method: XOR all numbers and indices
  • Edge cases: Empty array
Matlab
% Find missing number
function result = findMissing(arr)
    n = length(arr) + 1;
    total = n * (n + 1) / 2;
    sumArr = sum(arr);
    result = total - sumArr;
end
disp(findMissing([1, 2, 4, 5, 6]))  % 3

How interviewers ask it“Coding-round task — find duplicates in MATLAB. Explain your approach before you write the code.”

How do you find duplicates in MATLAB?

CodingMATLAB
Quick answerFind duplicates using unique and accumarray.
  • Method: unique with counts
  • Filter: counts > 1
  • Complexity: O(n log n)
Matlab
% Find duplicates
function result = findDuplicates(arr)
    [uniqueVals, ~, idx] = unique(arr);
    counts = accumarray(idx, 1);
    result = uniqueVals(counts > 1);
end
disp(findDuplicates([1, 2, 3, 2, 4, 3]))  % [2, 3]

% Manual implementation
function result = findDuplicatesManual(arr)
    seen = [];
    duplicates = [];
    for i = 1:length(arr)
        if any(seen == arr(i))
            duplicates = [duplicates, arr(i)];
        else
            seen = [seen, arr(i)];
        end
    end
    result = unique(duplicates);
end

How interviewers ask it“Live coding: Sum of array in MATLAB. Talk me through your approach first.”

How do you sum of array in MATLAB?

CodingMATLAB
Quick answerCalculate sum using sum or manual iteration.
  • Built-in: sum(arr)
  • Manual: Loop and accumulate
  • Empty array: Returns 0
Matlab
% Sum of array
function result = sumArray(arr)
    result = sum(arr);
end
disp(sumArray([1, 2, 3, 4, 5]))  % 15

% Manual implementation
function result = sumArrayManual(arr)
    result = 0;
    for i = 1:length(arr)
        result = result + arr(i);
    end
end

How interviewers ask it“Show me average of array in MATLAB — write it out and explain each step as you go.”

Explain average of array in MATLAB with an example.

CodingMATLAB
Quick answerCalculate average using mean or manual division.
  • Built-in: mean(arr)
  • Manual: sum(arr) / length(arr)
  • Empty array: Returns NaN
Matlab
% Average of array
function result = averageArray(arr)
    result = mean(arr);
end
disp(averageArray([1, 2, 3, 4, 5]))  % 3

% Manual implementation
function result = averageArrayManual(arr)
    result = sum(arr) / length(arr);
end

How interviewers ask it“Live coding: Sort array ascending in MATLAB. Talk me through your approach first.”

How do you sort array ascending in MATLAB?

CodingMATLAB

Sort using sort.

  • Built-in: sort(arr)
  • Non-mutating: sort returns new array
  • Complexity: O(n log n)
Matlab
% Sort array ascending
function result = sortAscending(arr)
    result = sort(arr);
end
disp(sortAscending([5, 2, 8, 1, 9]))  % [1, 2, 5, 8, 9]

% In-place sorting
function sortAscendingInPlace(arr)
    sort(arr);
end

How interviewers ask it“Coding-round task — sort array descending in MATLAB. Explain your approach before you write the code.”

How do you sort array descending in MATLAB?

CodingMATLAB
Quick answerSort descending using sort with 'descend'.
  • Built-in: sort(arr, 'descend')
  • Alternative: sort(arr, 'descend')
  • Complexity: O(n log n)
Matlab
% Sort array descending
function result = sortDescending(arr)
    result = sort(arr, 'descend');
end
disp(sortDescending([5, 2, 8, 1, 9]))  % [9, 8, 5, 2, 1]

% In-place sorting
function sortDescendingInPlace(arr)
    sort(arr, 'descend');
end

How interviewers ask it“Live coding: Flatten nested array in MATLAB. Talk me through your approach first.”

How do you flatten nested array in MATLAB?

CodingMATLAB
Quick answerFlatten using recursion or cell2mat.
  • Recursive: Check if cell
  • cell2mat: For numeric cells
  • Complexity: O(n) time
Matlab
% Flatten nested array
function result = flattenArray(arr)
    result = [];
    for i = 1:length(arr)
        if iscell(arr{i})
            result = [result, flattenArray(arr{i})];
        else
            result = [result, arr{i}];
        end
    end
end
disp(flattenArray({1, {2, {3, 4}, 5}, 6}))  % [1, 2, 3, 4, 5, 6]

% Using cellfun
function result = flattenArrayCell(arr)
    result = arr(cellfun(@iscell, arr));
    if ~isempty(result)
        result = flattenArray(result);
    end
end

How interviewers ask it“Show me chunk array in MATLAB — write it out and explain each step as you go.”

Explain chunk array in MATLAB with an example.

CodingMATLAB
Quick answerSplit array into chunks using mat2cell or manual slicing.
  • mat2cell: mat2cell(arr, 1, indices)
  • Manual: Loop and slice
  • Use case: Batch processing
Matlab
% Chunk array
function result = chunkArray(arr, size)
    result = {};
    for i = 1:size:length(arr)
        endIdx = min(i + size - 1, length(arr));
        result{end+1} = arr(i:endIdx);
    end
end
disp(chunkArray([1, 2, 3, 4, 5, 6], 2))  % {[1, 2], [3, 4], [5, 6]}

% Using mat2cell
function result = chunkArrayMat2cell(arr, size)
    n = length(arr);
    indices = diff([0, size:size:n]);
    result = mat2cell(arr, 1, indices);
end

How interviewers ask it“Show me quick sort in MATLAB — write it out and explain each step as you go.”

Explain quick sort in MATLAB with an example.

CodingMATLAB
Quick answerQuick sort using pivot-based partitioning.
  • Algorithm: Choose pivot, partition, recurse
  • Time: O(n log n) average
  • Implementation: Recursive
Matlab
% Quick sort
function result = quickSort(arr)
    if length(arr) <= 1
        result = arr;
        return;
    end
    pivot = arr(1);
    left = arr(arr < pivot);
    right = arr(arr > pivot);
    result = [quickSort(left), pivot, quickSort(right)];
end
disp(quickSort([5, 3, 8, 4, 2, 7, 1, 6]))

% In-place quick sort
function quickSortInPlace(arr, low, high)
    if nargin < 2
        low = 1;
        high = length(arr);
    end
    if low < high
        pi = partition(arr, low, high);
        quickSortInPlace(arr, low, pi - 1);
        quickSortInPlace(arr, pi + 1, high);
    end
end

function pi = partition(arr, low, high)
    pivot = arr(high);
    i = low - 1;
    for j = low:high-1
        if arr(j) <= pivot
            i = i + 1;
            temp = arr(i);
            arr(i) = arr(j);
            arr(j) = temp;
        end
    end
    temp = arr(i + 1);
    arr(i + 1) = arr(high);
    arr(high) = temp;
    pi = i + 1;
end

How interviewers ask it“Coding-round task — merge sort in MATLAB. Explain your approach before you write the code.”

How do you merge sort in MATLAB?

CodingMATLAB
Quick answerMerge sort using divide-and-conquer.
  • Algorithm: Divide, sort, merge
  • Time: O(n log n)
  • Space: O(n) auxiliary space
Matlab
% Merge sort
function result = mergeSort(arr)
    if length(arr) <= 1
        result = arr;
        return;
    end
    mid = floor(length(arr) / 2);
    left = mergeSort(arr(1:mid));
    right = mergeSort(arr(mid+1:end));
    result = merge(left, right);
end

function result = merge(left, right)
    i = 1;
    j = 1;
    result = [];
    while i <= length(left) && j <= length(right)
        if left(i) <= right(j)
            result = [result, left(i)];
            i = i + 1;
        else
            result = [result, right(j)];
            j = j + 1;
        end
    end
    if i <= length(left)
        result = [result, left(i:end)];
    end
    if j <= length(right)
        result = [result, right(j:end)];
    end
end

How interviewers ask it“Show me bubble sort in MATLAB — write it out and explain each step as you go.”

Explain bubble sort in MATLAB with an example.

CodingMATLAB
Quick answerBubble sort with early termination.
  • Algorithm: Compare adjacent, swap
  • Time: O(n²) worst case
  • Optimization: Stop if no swaps
Matlab
% Bubble sort
function result = bubbleSort(arr)
    result = arr;
    for i = 1:length(result) - 1
        for j = 1:length(result) - i
            if result(j) > result(j + 1)
                temp = result(j);
                result(j) = result(j + 1);
                result(j + 1) = temp;
            end
        end
    end
end

% Optimized bubble sort
function result = bubbleSortOptimized(arr)
    result = arr;
    for i = 1:length(result) - 1
        swapped = false;
        for j = 1:length(result) - i
            if result(j) > result(j + 1)
                temp = result(j);
                result(j) = result(j + 1);
                result(j + 1) = temp;
                swapped = true;
            end
        end
        if ~swapped
            break;
        end
    end
end

How interviewers ask it“Show me intersection of arrays in MATLAB — write it out and explain each step as you go.”

Explain intersection of arrays in MATLAB with an example.

CodingMATLAB
Quick answerFind common elements using intersect.
  • Built-in: intersect(arr1, arr2)
  • Alternative: ismember
  • Complexity: O(n log n)
Matlab
% Intersection of arrays
function result = intersection(arr1, arr2)
    result = intersect(arr1, arr2);
end
disp(intersection([1, 2, 3, 4], [3, 4, 5, 6]))  % [3, 4]

% Using ismember
function result = intersectionIsmember(arr1, arr2)
    result = arr1(ismember(arr1, arr2));
end

How interviewers ask it“Show me union of arrays in MATLAB — write it out and explain each step as you go.”

Explain union of arrays in MATLAB with an example.

CodingMATLAB
Quick answerCombine arrays with unique elements using union.
  • Built-in: union(arr1, arr2)
  • Alternative: unique([arr1, arr2])
  • Complexity: O(n log n)
Matlab
% Union of arrays
function result = unionArrays(arr1, arr2)
    result = union(arr1, arr2);
end
disp(unionArrays([1, 2, 3], [3, 4, 5]))  % [1, 2, 3, 4, 5]

% Using setdiff
function result = unionSetdiff(arr1, arr2)
    result = [arr1, setdiff(arr2, arr1)];
end
CodingMATLAB
Quick answerFind elements in first array not in second using setdiff.
  • Built-in: setdiff(arr1, arr2)
  • Symmetric: setdiff(arr1, arr2) ∪ setdiff(arr2, arr1)
  • Complexity: O(n log n)
Matlab
% Difference of arrays
function result = difference(arr1, arr2)
    result = setdiff(arr1, arr2);
end
disp(difference([1, 2, 3, 4], [3, 4, 5, 6]))  % [1, 2]

% Symmetric difference
function result = symmetricDifference(arr1, arr2)
    result = [setdiff(arr1, arr2), setdiff(arr2, arr1)];
end

How interviewers ask it“Can you group by property in MATLAB? Then walk me through the edge cases you would test.”

How do you group by property in MATLAB?

CodingMATLAB
Quick answerGroup structures by property using containers.Map.
  • Method: containers.Map
  • Key: Property value
  • Value: Array of structures
Matlab
% Group by property
function result = groupByProperty(items, key)
    result = containers.Map();
    for i = 1:length(items)
        if isfield(items(i), key)
            keyValue = items(i).(key);
            if isKey(result, keyValue)
                result(keyValue) = [result(keyValue), items(i)];
            else
                result(keyValue) = items(i);
            end
        end
    end
end

% Usage
data(1).type = 'fruit';
data(1).name = 'apple';
data(2).type = 'fruit';
data(2).name = 'banana';
data(3).type = 'veg';
data(3).name = 'carrot';

groups = groupByProperty(data, 'type');
keys = groups.keys();
for i = 1:length(keys)
    disp([keys{i} ': ' num2str(length(groups(keys{i}))) ' items'])
end

How interviewers ask it“Live coding: Deep clone object in MATLAB. Talk me through your approach first.”

How do you deep clone object in MATLAB?

CodingMATLAB
Quick answerDeep clone by recursively copying structures.
  • Method: Recursive function
  • Structures: Copy all fields
  • Cell arrays: Copy all elements
Matlab
% Deep clone object
function result = deepClone(obj)
    if isstruct(obj)
        result = struct();
        fields = fieldnames(obj);
        for i = 1:length(fields)
            result.(fields{i}) = deepClone(obj.(fields{i}));
        end
    elseif iscell(obj)
        result = cell(size(obj));
        for i = 1:numel(obj)
            result{i} = deepClone(obj{i});
        end
    else
        result = obj;
    end
end

% Usage
person.name = 'Alice';
person.address.city = 'NYC';
person.address.zip = '10001';

cloned = deepClone(person);
cloned.name = 'Bob';
disp(person.name)  % Alice
disp(cloned.name)  % Bob

How interviewers ask it“Show me immutable update in MATLAB — write it out and explain each step as you go.”

Explain immutable update in MATLAB with an example.

CodingMATLAB
Quick answerPerform immutable updates on nested structures.
  • Method: Copy and update path
  • Path: Dot notation
  • Use case: Functional programming
Matlab
% Immutable update
function result = updateImmutable(obj, path, value)
    parts = strsplit(path, '.');
    if length(parts) == 1
        result = obj;
        result.(parts{1}) = value;
    else
        result = obj;
        result.(parts{1}) = updateImmutable(obj.(parts{1}), ...
            strjoin(parts(2:end), '.'), value);
    end
end

state.user.name = 'Alice';
state.user.age = 25;
newState = updateImmutable(state, 'user.age', 26);
disp(state.user.age)  % 25
disp(newState.user.age)  % 26

How interviewers ask it“Show me pipe function in MATLAB — write it out and explain each step as you go.”

Explain pipe function in MATLAB with an example.

CodingMATLAB
Quick answerPipe composes functions from left to right.
  • Method: pipe(fns...)
  • Implementation: Loop with function handles
  • Direction: Left to right
Matlab
% Pipe function
function result = pipe(varargin)
    fns = varargin;
    result = @(value) applyPipe(value, fns);
end

function value = applyPipe(value, fns)
    for i = 1:length(fns)
        value = fns{i}(value);
    end
end

% Usage
double = @(x) x * 2;
addTen = @(x) x + 10;
square = @(x) x^2;

process = pipe(double, addTen, square);
disp(process(5))  % (5*2+10)^2 = 400

How interviewers ask it“Show me compose function in MATLAB — write it out and explain each step as you go.”

Explain compose function in MATLAB with an example.

CodingMATLAB
Quick answerCompose functions from right to left.
  • Method: compose(fns...)
  • Implementation: Loop with function handles
  • Direction: Right to left
Matlab
% Compose function
function result = compose(varargin)
    fns = varargin;
    result = @(value) applyCompose(value, fns);
end

function value = applyCompose(value, fns)
    for i = length(fns):-1:1
        value = fns{i}(value);
    end
end

% Usage
process2 = compose(square, addTen, double);
disp(process2(5))  % (5*2+10)^2 = 400

How interviewers ask it“Show me memoization in MATLAB — write it out and explain each step as you go.”

Explain memoization in MATLAB with an example.

CodingMATLAB
Quick answerCache function results based on arguments.
  • Method: containers.Map
  • Key: Arguments as string
  • Trade-off: Memory for speed
Matlab
% Memoization
function memoFn = memoize(fn)
    cache = containers.Map();
    memoFn = @(arg) memoizedCall(arg);
    
    function result = memoizedCall(arg)
        key = mat2str(arg);
        if isKey(cache, key)
            result = cache(key);
        else
            result = fn(arg);
            cache(key) = result;
        end
    end
end

% Usage
fibonacciMemo = memoize(@(n) (n <= 1) * n + (n > 1) * ...
    (fibonacciMemo(n - 1) + fibonacciMemo(n - 2)));
disp(fibonacciMemo(10))

How interviewers ask it“Show me once function in MATLAB — write it out and explain each step as you go.”

Explain once function in MATLAB with an example.

CodingMATLAB
Quick answerEnsure a function is called only once.
  • Method: Use flag and closure
  • Implementation: Track if called
  • Use case: Initialization
Matlab
% Once function
function onceFn = once(fn)
    called = false;
    result = [];
    onceFn = @() onceCall();
    
    function value = onceCall()
        if ~called
            called = true;
            result = fn();
        end
        value = result;
    end
end

% Usage
initialize = once(@() (disp('Initialized'), struct('id', 1, 'name', 'App')));
disp(initialize())  % Prints "Initialized"
disp(initialize())  % Returns cached result

How interviewers ask it“Live coding: Debounce with leading edge in MATLAB. Talk me through your approach first.”

How do you debounce with leading edge in MATLAB?

CodingMATLAB
Quick answerDebounce with leading edge executes immediately then waits.
  • Method: Track last call time
  • Implementation: Timer and flag
  • Use case: Save actions, API calls
Matlab
% Debounce with leading edge
function debounced = debounceLeading(delayMs, fn)
    lastCall = 0;
    timer = [];
    debounced = @() debounceCall();
    
    function debounceCall()
        now = toc();
        if now - lastCall < delayMs / 1000
            if ~isempty(timer)
                stop(timer)
                delete(timer)
            end
            timer = timer('StartDelay', delayMs / 1000, ...
                'TimerFcn', @(~,~) (lastCall = toc(); fn()));
            start(timer)
        else
            lastCall = now;
            fn()
        end
    end
end

How interviewers ask it“Coding-round task — throttle with leading edge in MATLAB. Explain your approach before you write the code.”

How do you throttle with leading edge in MATLAB?

CodingMATLAB
Quick answerThrottle with leading edge executes at most once per time period.
  • Method: Track last call time
  • Implementation: Check time difference
  • Use case: Scroll events
Matlab
% Throttle with leading edge
function throttled = throttleLeading(delayMs, fn)
    lastCall = 0;
    throttled = @() throttleCall();
    
    function throttleCall()
        now = toc();
        if now - lastCall >= delayMs / 1000
            lastCall = now;
            fn()
        end
    end
end

How interviewers ask it“Coding-round task — deep equal in MATLAB. Explain your approach before you write the code.”

How do you deep equal in MATLAB?

CodingMATLAB
Quick answerDeep equality comparison for nested structures.
  • Method: Recursive comparison
  • Structures: Compare fields
  • Cell arrays: Compare elements
Matlab
% Deep equal
function result = deepEqual(obj1, obj2)
    if isequal(obj1, obj2)
        result = true;
        return;
    end
    if isstruct(obj1) && isstruct(obj2)
        fields1 = fieldnames(obj1);
        fields2 = fieldnames(obj2);
        if ~isequal(fields1, fields2)
            result = false;
            return;
        end
        for i = 1:length(fields1)
            if ~deepEqual(obj1.(fields1{i}), obj2.(fields2{i}))
                result = false;
                return;
            end
        end
        result = true;
    elseif iscell(obj1) && iscell(obj2)
        if length(obj1) ~= length(obj2)
            result = false;
            return;
        end
        for i = 1:length(obj1)
            if ~deepEqual(obj1{i}, obj2{i})
                result = false;
                return;
            end
        end
        result = true;
    else
        result = isequal(obj1, obj2);
    end
end

How interviewers ask it“Show me observable pattern in MATLAB — write it out and explain each step as you go.”

Explain observable pattern in MATLAB with an example.

CodingMATLAB
Quick answerObservable pattern for event notification.
  • Observable: Maintains subscribers
  • Subscribe: Add callback
  • Notify: Call all subscribers
Matlab
% Observable pattern
function observable = createObservable()
    observable.subscribers = {};
    observable.subscribe = @(callback) addSubscriber(callback);
    observable.notify = @(data) notifySubscribers(data);
    
    function unsubscribe = addSubscriber(callback)
        observable.subscribers{end+1} = callback;
        unsubscribe = @() removeSubscriber(callback);
    end
    
    function removeSubscriber(callback)
        idx = find(cellfun(@(x) isequal(x, callback), observable.subscribers));
        observable.subscribers(idx) = [];
    end
    
    function notifySubscribers(data)
        for i = 1:length(observable.subscribers)
            observable.subscribers{i}(data);
        end
    end
end

% Usage
obs = createObservable();
unsubscribe = obs.subscribe(@(data) disp(['Received: ' data]));
obs.notify('Hello')  % Received: Hello
unsubscribe()
obs.notify('World')  % Nothing happens

How interviewers ask it“Show me singleton pattern in MATLAB — write it out and explain each step as you go.”

Explain singleton pattern in MATLAB with an example.

CodingMATLAB
Quick answerSingleton pattern using persistent variable.
  • Method: Persistent variable
  • Thread-safe: Single-threaded
  • Global access: Through function
Matlab
% Singleton pattern
function singleton = getSingleton()
    persistent instance
    if isempty(instance)
        instance.data = containers.Map();
        instance.set = @(key, value) setData(key, value);
        instance.get = @(key) getData(key);
        singleton = instance;
    else
        singleton = instance;
    end
    
    function setData(key, value)
        instance.data(key) = value;
    end
    
    function value = getData(key)
        if isKey(instance.data, key)
            value = instance.data(key);
        else
            value = [];
        end
    end
end

% Usage
singleton = getSingleton();
singleton.set('name', 'Alice');
disp(singleton.get('name'))  % Alice

How interviewers ask it“Show me factory pattern in MATLAB — write it out and explain each step as you go.”

Explain factory pattern in MATLAB with an example.

CodingMATLAB
Quick answerFactory pattern using switch statement.
  • Method: createUser function
  • Benefits: Decouples creation
  • Structures: Return different types
Matlab
% Factory pattern
function user = createUser(type, name)
    switch type
        case 'admin'
            user = struct('type', 'admin', 'name', name);
        case 'guest'
            user = struct('type', 'guest', 'name', name);
        otherwise
            user = struct('type', 'regular', 'name', name);
    end
end

% Usage
admin = createUser('admin', 'Alice');
disp(admin)

How interviewers ask it“Show me strategy pattern in MATLAB — write it out and explain each step as you go.”

Explain strategy pattern in MATLAB with an example.

CodingMATLAB
Quick answerStrategy pattern using function handles.
  • Interface: Function handle
  • Context: Uses strategy
  • Benefits: Runtime switching
Matlab
% Strategy pattern
function strategy = createPaymentStrategy(type)
    switch type
        case 'credit'
            strategy.pay = @(amount) disp(['Paid $' num2str(amount) ' with Credit Card']);
        case 'paypal'
            strategy.pay = @(amount) disp(['Paid $' num2str(amount) ' with PayPal']);
        case 'crypto'
            strategy.pay = @(amount) disp(['Paid $' num2str(amount) ' with Crypto']);
        otherwise
            strategy.pay = @(amount) disp(['Paid $' num2str(amount) ' with Unknown']);
    end
end

function executePayment(strategy, amount)
    strategy.pay(amount);
end

% Usage
credit = createPaymentStrategy('credit');
paypal = createPaymentStrategy('paypal');
executePayment(credit, 100);
executePayment(paypal, 50);

How interviewers ask it“Show me observer pattern in MATLAB — write it out and explain each step as you go.”

Explain observer pattern in MATLAB with an example.

CodingMATLAB
Quick answerObserver pattern using functions.
  • Subject: Maintains observers
  • Observer: Receives updates
  • Benefits: Loose coupling
Matlab
% Observer pattern
function subject = createSubject()
    subject.observers = {};
    subject.state = '';
    subject.attach = @(observer) attachObserver(observer);
    subject.detach = @(observer) detachObserver(observer);
    subject.setState = @(newState) setState(newState);
    
    function attachObserver(observer)
        subject.observers{end+1} = observer;
    end
    
    function detachObserver(observer)
        idx = find(cellfun(@(x) isequal(x, observer), subject.observers));
        subject.observers(idx) = [];
    end
    
    function setState(newState)
        subject.state = newState;
        notifyObservers();
    end
    
    function notifyObservers()
        for i = 1:length(subject.observers)
            subject.observers{i}.update(subject.state);
        end
    end
end

function observer = createObserver(name)
    observer.name = name;
    observer.update = @(data) disp([name ' received: ' data]);
end

% Usage
subject = createSubject();
observer1 = createObserver('Observer1');
observer2 = createObserver('Observer2');
subject.attach(observer1);
subject.attach(observer2);
subject.setState('Hello World')

How interviewers ask it“Show me decorator pattern in MATLAB — write it out and explain each step as you go.”

Explain decorator pattern in MATLAB with an example.

CodingMATLAB
Quick answerDecorator pattern using wrapper functions.
  • Component: Base object
  • Decorator: Wraps component
  • Benefits: Flexible extension
Matlab
% Decorator pattern
function coffee = createCoffee()
    coffee.cost = 5.0;
    coffee.description = 'Coffee';
end

function coffee = milkDecorator(coffee)
    coffee.cost = coffee.cost + 2.0;
    coffee.description = [coffee.description ', Milk'];
end

function coffee = sugarDecorator(coffee)
    coffee.cost = coffee.cost + 1.0;
    coffee.description = [coffee.description ', Sugar'];
end

% Usage
coffee = createCoffee();
coffee = milkDecorator(coffee);
coffee = sugarDecorator(coffee);
disp(coffee.description)  % Coffee, Milk, Sugar
disp(coffee.cost)  % 8.0

How interviewers ask it“Show me command pattern in MATLAB — write it out and explain each step as you go.”

Explain command pattern in MATLAB with an example.

CodingMATLAB
Quick answerCommand pattern using functions.
  • Command: Encapsulates request
  • Invoker: Executes commands
  • Benefits: Undo/redo
Matlab
% Command pattern
function command = createAddCommand(receiver, value)
    command.execute = @() executeAdd();
    command.undo = @() undoAdd();
    
    function executeAdd()
        receiver(end+1) = value;
    end
    
    function undoAdd()
        receiver(end) = [];
    end
end

% Usage
receiver = [1, 2, 3];
cmd = createAddCommand(receiver, 4);
cmd.execute()
disp(receiver)  % [1, 2, 3, 4]
cmd.undo()
disp(receiver)  % [1, 2, 3]

How interviewers ask it“Show me memento pattern in MATLAB — write it out and explain each step as you go.”

Explain memento pattern in MATLAB with an example.

CodingMATLAB
Quick answerMemento pattern for state restoration.
  • Originator: Creates/restores mementos
  • Memento: Stores state
  • Caretaker: Manages mementos
Matlab
% Memento pattern
function memento = createMemento(state)
    memento.state = state;
end

function originator = createOriginator()
    originator.state = '';
    originator.saveState = @() createMemento(originator.state);
    originator.restoreState = @(memento) setState(memento.state);
    
    function setState(newState)
        originator.state = newState;
        disp(['State set to: ' newState])
    end
end

function caretaker = createCaretaker()
    caretaker.mementos = {};
    caretaker.addMemento = @(memento) addMemento(memento);
    caretaker.getMemento = @(index) getMemento(index);
    
    function addMemento(memento)
        caretaker.mementos{end+1} = memento;
    end
    
    function memento = getMemento(index)
        memento = caretaker.mementos{index};
    end
end

% Usage
originator = createOriginator();
caretaker = createCaretaker();

originator.state = 'State 1';
caretaker.addMemento(originator.saveState());
originator.state = 'State 2';
caretaker.addMemento(originator.saveState());
originator.state = 'State 3';

originator.restoreState(caretaker.getMemento(1));
disp(originator.state)  % State 1

How interviewers ask it“Show me mediator pattern in MATLAB — write it out and explain each step as you go.”

Explain mediator pattern in MATLAB with an example.

CodingMATLAB
Quick answerMediator pattern for centralized communication.
  • Mediator: Encapsulates communication
  • Colleague: Communicates through mediator
  • Benefits: Loose coupling
Matlab
% Mediator pattern
function mediator = createMediator()
    mediator.colleagues = {};
    mediator.register = @(colleague) registerColleague(colleague);
    mediator.send = @(message, sender) sendMessage(message, sender);
    
    function registerColleague(colleague)
        mediator.colleagues{end+1} = colleague;
    end
    
    function sendMessage(message, sender)
        for i = 1:length(mediator.colleagues)
            if ~isequal(mediator.colleagues{i}, sender)
                mediator.colleagues{i}.receive(message);
            end
        end
    end
end

function colleague = createColleague(name, mediator)
    colleague.name = name;
    colleague.mediator = mediator;
    colleague.send = @(message) sendMessage(message);
    colleague.receive = @(message) receiveMessage(message);
    mediator.register(colleague);
    
    function sendMessage(message)
        mediator.send(message, colleague);
    end
    
    function receiveMessage(message)
        disp([name ' received: ' message])
    end
end

% Usage
mediator = createMediator();
alice = createColleague('Alice', mediator);
bob = createColleague('Bob', mediator);
alice.send('Hello Bob!')

How interviewers ask it“Show me chain of Responsibility in MATLAB — write it out and explain each step as you go.”

Explain chain of Responsibility in MATLAB with an example.

CodingMATLAB
Quick answerChain of Responsibility using functions.
  • Handler: Processes or forwards
  • Chain: Linked list of handlers
  • Benefits: Decoupling
Matlab
% Chain of Responsibility
function handler = createHandler()
    handler.nextHandler = [];
    handler.setNext = @(next) setNextHandler(next);
    handler.handle = @(request) handleRequest(request);
end

function setNextHandler(handler, next)
    handler.nextHandler = next;
end

function authHandler = createAuthHandler()
    authHandler = createHandler();
    authHandler.handle = @(request) handleAuth(request);
    
    function handleAuth(request)
        if isfield(request, 'token')
            disp('Authentication passed')
            if ~isempty(authHandler.nextHandler)
                authHandler.nextHandler.handle(request);
            end
        else
            disp('Authentication failed')
        end
    end
end

function loggerHandler = createLoggerHandler()
    loggerHandler = createHandler();
    loggerHandler.handle = @(request) handleLog(request);
    
    function handleLog(request)
        disp(['Logging request: ' request.url])
        if ~isempty(loggerHandler.nextHandler)
            loggerHandler.nextHandler.handle(request);
        end
    end
end

% Usage
auth = createAuthHandler();
logger = createLoggerHandler();
auth.setNext(logger);
auth.handle(struct('token', 'valid', 'url', '/api'))

How interviewers ask it“Show me state pattern in MATLAB — write it out and explain each step as you go.”

Explain state pattern in MATLAB with an example.

CodingMATLAB
Quick answerState pattern using switch or functions.
  • Context: Maintains state
  • State: Defines behavior
  • Benefits: Clean state management
Matlab
% State pattern
function context = createContext()
    context.state = 'ready';
    context.request = @() handleRequest();
    
    function handleRequest()
        switch context.state
            case 'ready'
                disp('Ready: Waiting for input')
            case 'processing'
                disp('Processing: Working on task')
            case 'completed'
                disp('Completed: Task finished')
        end
    end
end

% Usage
context = createContext();
context.request()  % Ready: Waiting for input
context.state = 'processing';
context.request()  % Processing: Working on task
context.state = 'completed';
context.request()  % Completed: Task finished

How interviewers ask it“Show me proxy pattern in MATLAB — write it out and explain each step as you go.”

Explain proxy pattern in MATLAB with an example.

CodingMATLAB
Quick answerProxy pattern using functions.
  • Subject: Real object
  • Proxy: Controls access
  • Benefits: Access control
Matlab
% Proxy pattern
function realSubject = createRealSubject()
    realSubject.request = @() disp('RealSubject: Handling request');
end

function proxy = createProxy()
    proxy.realSubject = [];
    proxy.request = @() proxyRequest();
    
    function proxyRequest()
        if checkAccess()
            if isempty(proxy.realSubject)
                proxy.realSubject = createRealSubject();
            end
            proxy.realSubject.request();
            logAccess();
        end
    end
    
    function access = checkAccess()
        disp('Proxy: Checking access')
        access = true;
    end
    
    function logAccess()
        disp('Proxy: Logging access')
    end
end

% Usage
proxy = createProxy();
proxy.request()

How interviewers ask it“Show me flyweight pattern in MATLAB — write it out and explain each step as you go.”

Explain flyweight pattern in MATLAB with an example.

CodingMATLAB
Quick answerFlyweight pattern for sharing objects.
  • Flyweight: Shared object
  • Factory: Manages flyweights
  • Benefits: Memory optimization
Matlab
% Flyweight pattern
function flyweight = createFlyweight(sharedState)
    flyweight.sharedState = sharedState;
    flyweight.operation = @(uniqueState) flyweightOperation(uniqueState);
    
    function flyweightOperation(uniqueState)
        disp(['Shared: ' sharedState ', Unique: ' uniqueState])
    end
end

function factory = createFlyweightFactory()
    factory.flyweights = containers.Map();
    factory.getFlyweight = @(sharedState) getFlyweight(sharedState);
    
    function fw = getFlyweight(sharedState)
        if isKey(factory.flyweights, sharedState)
            fw = factory.flyweights(sharedState);
        else
            fw = createFlyweight(sharedState);
            factory.flyweights(sharedState) = fw;
        end
    end
end

% Usage
factory = createFlyweightFactory();
fw1 = factory.getFlyweight('state1');
fw2 = factory.getFlyweight('state1');
fw3 = factory.getFlyweight('state2');
fw1.operation('unique1');
fw2.operation('unique2');
fw3.operation('unique3')

How interviewers ask it“Show me bridge pattern in MATLAB — write it out and explain each step as you go.”

Explain bridge pattern in MATLAB with an example.

CodingMATLAB
Quick answerBridge pattern for separating abstraction from implementation.
  • Abstraction: High-level interface
  • Implementation: Low-level operations
  • Benefits: Separation of concerns
Matlab
% Bridge pattern
function implA = createImplementationA()
    implA.operation = @() disp('ConcreteImplementationA: Operation');
end

function implB = createImplementationB()
    implB.operation = @() disp('ConcreteImplementationB: Operation');
end

function abstraction = createAbstraction(impl)
    abstraction.impl = impl;
    abstraction.operation = @() abstractionOperation();
    
    function abstractionOperation()
        disp('Abstraction: Additional logic')
        abstraction.impl.operation();
    end
end

% Usage
implA = createImplementationA();
implB = createImplementationB();
abstraction1 = createAbstraction(implA);
abstraction2 = createAbstraction(implB);
abstraction1.operation();
abstraction2.operation()

How interviewers ask it“Show me adapter pattern in MATLAB — write it out and explain each step as you go.”

Explain adapter pattern in MATLAB with an example.

CodingMATLAB
Quick answerAdapter pattern for converting interfaces.
  • Target: Expected interface
  • Adaptee: Existing interface
  • Adapter: Bridges interfaces
Matlab
% Adapter pattern
function target = createTarget()
    target.request = @() disp('Target: Request');
end

function adaptee = createAdaptee()
    adaptee.specificRequest = @() disp('Adaptee: Specific Request');
end

function adapter = createAdapter(adaptee)
    adapter.adaptee = adaptee;
    adapter.request = @() adapterRequest();
    
    function adapterRequest()
        adaptee.specificRequest();
    end
end

% Usage
adaptee = createAdaptee();
adapter = createAdapter(adaptee);
adapter.request()

How interviewers ask it“Show me facade pattern in MATLAB — write it out and explain each step as you go.”

Explain facade pattern in MATLAB with an example.

CodingMATLAB
Quick answerFacade pattern for simplifying subsystems.
  • Facade: Simplified interface
  • Subsystem: Complex components
  • Benefits: Simplified interface
Matlab
% Facade pattern
function subsystemA = createSubsystemA()
    subsystemA.operation = @() disp('SubsystemA: Operation');
end

function subsystemB = createSubsystemB()
    subsystemB.operation = @() disp('SubsystemB: Operation');
end

function facade = createFacade()
    facade.subsystemA = createSubsystemA();
    facade.subsystemB = createSubsystemB();
    facade.operation = @() facadeOperation();
    
    function facadeOperation()
        facade.subsystemA.operation();
        facade.subsystemB.operation();
        disp('Facade: Complex operation')
    end
end

% Usage
facade = createFacade();
facade.operation()

How interviewers ask it“Show me composite pattern in MATLAB — write it out and explain each step as you go.”

Explain composite pattern in MATLAB with an example.

CodingMATLAB
Quick answerComposite pattern for tree structures.
  • Component: Interface for all
  • Leaf: Individual object
  • Composite: Container
Matlab
% Composite pattern
function leaf = createLeaf(name)
    leaf.name = name;
    leaf.operation = @() disp(['Leaf ' name ': Operation']);
end

function composite = createComposite(name)
    composite.name = name;
    composite.children = {};
    composite.add = @(component) addComponent(component);
    composite.remove = @(component) removeComponent(component);
    composite.operation = @() compositeOperation();
    
    function addComponent(component)
        composite.children{end+1} = component;
    end
    
    function removeComponent(component)
        idx = find(cellfun(@(x) isequal(x, component), composite.children));
        composite.children(idx) = [];
    end
    
    function compositeOperation()
        disp(['Composite ' name ': Operation'])
        for i = 1:length(composite.children)
            composite.children{i}.operation();
        end
    end
end

% Usage
leaf1 = createLeaf('A');
leaf2 = createLeaf('B');
composite = createComposite('Root');
composite.add(leaf1);
composite.add(leaf2);
composite.operation()

How interviewers ask it“Show me visitor pattern in MATLAB — write it out and explain each step as you go.”

Explain visitor pattern in MATLAB with an example.

CodingMATLAB
Quick answerVisitor pattern for adding operations.
  • Visitor: Defines operations
  • Element: Accepts visitors
  • Benefits: Adding operations without modifying
Matlab
% Visitor pattern
function elementA = createElementA()
    elementA.accept = @(visitor) visitor.visitA(elementA);
end

function elementB = createElementB()
    elementB.accept = @(visitor) visitor.visitB(elementB);
end

function visitor = createVisitor()
    visitor.visitA = @(element) disp('Visiting ElementA');
    visitor.visitB = @(element) disp('Visiting ElementB');
end

% Usage
visitor = createVisitor();
elementA = createElementA();
elementB = createElementB();
elementA.accept(visitor);
elementB.accept(visitor)

How interviewers ask it“Show me iterator pattern in MATLAB — write it out and explain each step as you go.”

Explain iterator pattern in MATLAB with an example.

CodingMATLAB
Quick answerIterator pattern for sequential access.
  • Iterator: Traverses collection
  • Aggregate: Creates iterator
  • Benefits: Uniform traversal
Matlab
% Iterator pattern
function iterator = createIterator(collection)
    iterator.collection = collection;
    iterator.index = 1;
    iterator.next = @() nextItem();
    iterator.hasNext = @() hasNextItem();
    
    function item = nextItem()
        if hasNextItem()
            item = iterator.collection{iterator.index};
            iterator.index = iterator.index + 1;
        else
            item = [];
        end
    end
    
    function result = hasNextItem()
        result = iterator.index <= length(iterator.collection);
    end
end

function collection = createCollection()
    collection.items = {};
    collection.add = @(item) addItem(item);
    collection.getIterator = @() createIterator(collection.items);
    
    function addItem(item)
        collection.items{end+1} = item;
    end
end

% Usage
collection = createCollection();
collection.add('A');
collection.add('B');
collection.add('C');
iterator = collection.getIterator();
while iterator.hasNext()
    disp(iterator.next())
end

How interviewers ask it“Show me template Method pattern in MATLAB — write it out and explain each step as you go.”

Explain template Method pattern in MATLAB with an example.

CodingMATLAB
Quick answerTemplate Method for algorithm skeletons.
  • AbstractClass: Defines template
  • ConcreteClass: Implements steps
  • Benefits: Code reuse
Matlab
% Template Method pattern
function abstractClass = createAbstractClass()
    abstractClass.templateMethod = @() templateMethod();
    abstractClass.step1 = @() disp('Step 1');
    abstractClass.step3 = @() disp('Step 3');
end

function concreteClass = createConcreteClass()
    concreteClass = createAbstractClass();
    concreteClass.step2 = @() disp('Concrete Step 2');
end

function templateMethod(obj)
    obj.step1();
    obj.step2();
    obj.step3();
end

% Usage
concrete = createConcreteClass();
concrete.templateMethod()

How interviewers ask it“Show me builder pattern in MATLAB — write it out and explain each step as you go.”

Explain builder pattern in MATLAB with an example.

CodingMATLAB
Quick answerBuilder pattern for constructing complex objects.
  • Builder: Constructs parts
  • Director: Orchestrates construction
  • Product: Constructed object
Matlab
% Builder pattern
function product = createProduct()
    product.parts = {};
    product.add = @(part) addPart(part);
    product.listParts = @() disp(strjoin(product.parts, ', '));
    
    function addPart(part)
        product.parts{end+1} = part;
    end
end

function builder = createBuilder()
    builder.product = createProduct();
    builder.reset = @() resetBuilder();
    builder.buildStepA = @() buildA();
    builder.buildStepB = @() buildB();
    builder.getResult = @() builder.product;
    
    function resetBuilder()
        builder.product = createProduct();
    end
    
    function buildA()
        builder.product.add('Part A');
    end
    
    function buildB()
        builder.product.add('Part B');
    end
end

function director = createDirector(builder)
    director.builder = builder;
    director.buildMinimal = @() buildMinimal();
    director.buildFull = @() buildFull();
    
    function buildMinimal()
        builder.buildStepA();
    end
    
    function buildFull()
        builder.buildStepA();
        builder.buildStepB();
    end
end

% Usage
builder = createBuilder();
director = createDirector(builder);
director.buildMinimal();
product = builder.getResult();
product.listParts()

How interviewers ask it“Show me prototype pattern in MATLAB — write it out and explain each step as you go.”

Explain prototype pattern in MATLAB with an example.

CodingMATLAB
Quick answerPrototype pattern for cloning objects.
  • Prototype: Cloneable object
  • Clone: Creates a copy
  • Benefits: Performance
Matlab
% Prototype pattern
function prototype = createPrototype(name, nested)
    prototype.name = name;
    prototype.nested = nested;
    prototype.clone = @() clonePrototype();
    prototype.deepClone = @() deepClonePrototype();
    
    function clone = clonePrototype()
        clone = createPrototype(prototype.name, prototype.nested);
    end
    
    function clone = deepClonePrototype()
        clone = createPrototype(prototype.name, deepCopy(prototype.nested));
    end
    
    function copied = deepCopy(obj)
        if isstruct(obj)
            copied = struct();
            fields = fieldnames(obj);
            for i = 1:length(fields)
                copied.(fields{i}) = deepCopy(obj.(fields{i}));
            end
        elseif iscell(obj)
            copied = cell(size(obj));
            for i = 1:numel(obj)
                copied{i} = deepCopy(obj{i});
            end
        else
            copied = obj;
        end
    end
end

% Usage
original = createPrototype('Original', struct('value', 42));
copy = original.clone();
copy.name = 'Copy';
copy.nested.value = 99;
disp(original.name)  % Original
disp(original.nested.value)  % 42 (shallow copy)

deepCopy = original.deepClone();
deepCopy.nested.value = 100;
disp(original.nested.value)  % 42 (deep copy)

Practise with the free MATLAB quiz

Multiple-choice questions with instant feedback. Takes about 10 minutes and shows exactly which concepts to revise.

Start the MATLAB quiz →

MATLAB interview FAQ

What is MATLAB?
MATLAB is a high-level programming language and numerical computing environment used for mathematical calculations, data analysis, visualization, simulation, machine learning, and engineering applications.
What are matrices in MATLAB?
Matrices are the fundamental data structure in MATLAB. They are used to store and process numerical data, perform mathematical operations, and implement algorithms.
What is the difference between scripts and functions in MATLAB?
A MATLAB script is a file containing a sequence of commands that share the workspace, while a function has its own workspace and accepts input arguments and returns output values.
What is Simulink in MATLAB?
Simulink is a MATLAB-based graphical simulation environment used for modeling, designing, and simulating dynamic systems such as control systems, signal processing systems, and embedded applications.
What are MATLAB arrays?
MATLAB arrays are data containers used to store numbers, characters, logical values, structures, and other types of data. MATLAB provides powerful operations for array manipulation and computation.
How is plotting performed in MATLAB?
MATLAB provides built-in plotting functions such as plot, scatter, bar, histogram, and surf to create 2D and 3D data visualizations.
What are MATLAB applications in machine learning?
MATLAB is used in machine learning for data preprocessing, feature extraction, model training, classification, regression, deep learning, and predictive analytics.
What is MATLAB used for in signal processing?
MATLAB is widely used for signal processing tasks such as filtering, Fourier transformation, image processing, audio analysis, and communication system simulation.
Why are MATLAB interview questions important?
MATLAB interview questions evaluate a candidate's understanding of numerical computing, programming concepts, data analysis, simulation, visualization, and engineering problem-solving skills.
How can I prepare for a MATLAB interview?
Practice MATLAB syntax, matrices, arrays, scripts, functions, plotting, Simulink concepts, data analysis techniques, machine learning algorithms, and solve real-world MATLAB coding problems.
AK
Written and reviewed by Abhishek Kumar
Founder, InterviewPitch · 10+ years in IT. Questions are collected from real interview rounds and refreshed regularly. Spotted an error? Tell us.