52.
MathWorks technical mcq question, verified with a worked answer. Free to practise - no sign-up.
52. Shader Loader
A game's shaders are rendered using two GPUs: a and b. There is a string s, which represents that for the ith shader in which a GPU is used.
- If shader[i] = 'a' then the GPU a is used for the ith shader.
- If shader[i] = 'b' then the GPU b is used in the ith shader.
The idleness of this dual GPU system is defined as the maximum number of shaders for which the same GPU is used consecutively. For example, for the string shader = "aabbba", for the first 2 seconds, GPU a is used, then for the next 3 seconds, GPU b is used, then for 1 second, GPU a is used. Hence, the idleness of the system is 3.
In order to reduce the idleness of the system, the following operation can be used at most switchCount times.
- Select any index i of the string shader. If shader[i] = 'a' then change it to shader[i] = 'b' and vice versa.
Find the minimum possible idleness of the system that can be achieved by applying the operations optimally.
Returns
int: the minimum possible idleness of the system that can be achieved by applying the operations optimally
Constraints
- 1 ≤ shader ≤ 2 × 10⁵
- 1 ≤ switchCount ≤ shader
It is guaranteed that shader consists of characters 'a' and 'b' only.
Example
Input:
STDIN FUNCTION
aaaaa → shader = "aaaaa"
1 → switchCount = 1
Output:
2
Explanation:
The optimal solution:
- Apply operation 1 to flip shader[2] from 'a' to 'b'. Thus, shader = "aabaa"
31. Window and Document Elements
Given the HTML:
<p id="test" name="hacker">Hello</p>
Which of the following JavaScript code snippets will append " Mr. Bob" after "Hello"?
Pick ONE OR MORE options
Show answer & explanation
getElementById() returns a single element directly, so no indexing is needed. Options B and D use getElementsByName() which returns a NodeList requiring [0] indexing. Option C incorrectly indexes getElementById() result. Only option A correctly appends to the element.
Step-by-step Derivation:
getElementById("test") → returns the
element directly
.innerHTML += " Mr. Bob" → appends " Mr. Bob" to existing HTML content "HelLo"
Result: "HelLo Mr. Bob"
Why others fail:
- Option B: getElementsByName() returns NodeList, not a single element; cannot use .innerHTML directly
- Option C: getElementById() returns an element, not an array; [0] indexing fails
- Option D: Works syntactically but uses getElementsByName() (less specific than getElementById)