given a matrix {
{ 'a', ' b', 'c','d' },
{ 'e','f', 'g', 'h' },
{ 'i', 'j', 'k', 'l' },
{'m', 'n', 'o', 'p'}
};
You are guaranteed the matrix will be N x N.
They wanted you to transform the 2d matrix (they called it a "wrapped array" ) in place, across the diagonal axis. So if the matrix was a paper you fold it across [a, f, k, p]. Leaving [a,f,k,p] unchanged.
Key points to remember: in-place transformation, and he wanted the function to be void.
In code speak, they wanted this
This is an O(N) solution since you walk every element in the matrix
class Solution{
public:
void transform(std::vector<std::vector<char>> & matrix){
for(size_t i = 0; i < matrix.size(); i++){
for(size_t j = 0; j < matrix.size(); j++){
if(j <= i) continue;
std::swap(matrix[i][j], matrix[j][i]);
}
}
}
};
But you can probably do better than this, since you only need to walk one side of the matrix.
class Solution{
public:
void transform(std::vector<std::vector<char>> & matrix){
for(size_t i = 0; i < matrix.size(); i++){
for(size_t j = i+1; j < matrix.size(); j++){
std::swap(matrix[i][j], matrix[j][i]);
}
}
}
};
They asked a million language spacific questions:
javascript what is a closure, difference between var and let (let bracket scoped and it is binding in for loops), dependency injection, imedieatly invoked functions, and why you would use them.
They didn't ask, but I also talked about javascript concurrency.
Tests: What can you write tests for, and what can't you write testes for. (broad conversation about the separation of concerns and anti-patterns, and good coding practices.
"What makes a good API" (this is another broad question about good coding practices, think more about what makes a good function): naming using verbs, use descriptive and appropriate names, parameters, errors, avoid side effects by not doing more than what is described. You should talk about pagination and fragmentation and how you can manage states when you need to send long responses. ( I didn't mention all these things, but you might find it helpful to know about them)
C++ question (I knew more than the interviewer, and talked at nauseam, so I didn't get too many questions here) just know the difference between pointer and reference.
Architecture questions: just know basic tools PostgreSQL, Redis, that was really about it. I was hoping they would ask about microservices architecture.