In recent times a meme has been making the rounds that allegedly shows a smart way to solve interview puzzles. In these memes developers are asked to create an ASCII pattern like the following:
XXXXXXX XX XX X X X X X X X X X X X XX XX XXXXXXX
The solution they come up with is a simple console.log() or print command.
console.log(XXXXXXX<br /> XX   XX<br /> X X X X<br /> X  X  X<br /> X X X X<br /> XX   XX<br /> <span class="caps">XXXXXXX</span>
)
Funny, of sorts, and it shows how limited interview questions like these are. Ask a stupid question, get a simple answer.
That’s why in CODE100 puzzles, I always used tons of data or asked people to create a pattern from a dataset. I make it annoying to type these things by hand – or provide erroneous examples – so that people have to start thinking.
Programming is about flexibility and preparation for change, not instant creation
This, to me, has always been the idea of programming: provide a flexible solution for a problem. Instead of “create this thing”, my first goal is always to “create a solution that can create this thing, but also derivates of it”.
I suppose being a web developer teaches you that from the get-go. As you have no idea what text or in what language you need to show content in CMS driven products, you create flexible solutions. There is no such thing as “This button is always one word, and not more than 10 characters”. So you make the layout grow and shrink with the need and the capability of the display device.
And in general this should be a thing to get used to as a developer: requirements will always change. No matter how detailed a specification is. No matter how accomplished the prototype looks. There will always be new things coming your way. That’s why the first reflex of a developer to a request like this should always be this:
“How can this change in the future, and what do I need to put in my code to be ready for it?”
Or, to put it in other words:
“This is one outcome, but how can I create something that creates this one, but also more generic ones?”
My solution to this would be to create a generic grid I can plot on. One way to do that is creating an array of Y values with each having an array of X values.
class ASCIIgrid {
grid = [];
create = (width, height, char) => {
for (let i = 0; i < height; i++) {
this.grid.push(Array(width).fill(char));
}
};
plot = (x, y, char) => {
if (y >= 0 && y < this.grid.length &&
x >= 0 && x < this.grid[y].length) {
this.grid[y][x] = char;
}
};
render = () => {
this.grid = this.grid.map(row => row.join(‘’));
return this.grid.join(‘n’);
};
}
This allows for a plot method to set a char at x and y, and a render method that returns the nested arrays as a string.
That way I can “paint” the solution and many others:
import { ASCIIgrid } from ‘./index.js’;
let c = new ASCIIgrid();
let w = 7;
let h = 7;
let char = ‘X’;
c.create(w, h, ‘ ’);
for(let i = 0; i < h; i++) {
c.plot(i, 0, char); // top line
c.plot((w – 1), i, char); // right line
c.plot(i, (h – 1), char); // bottom line
c.plot(0, i, char); // left line
c.plot(i, i, char); // downward left to right
c.plot(i, (h – 1) – i, char); // downward right to left
}
console.log(c.render());
Overkill? Maybe, but I could now easily create the same pattern for 9 chars, 27 chars or 101 simply by changing the w and h variables. I could also just paint an X, a smiley, a sine wave, or whatever. I don’t expect someone in an interview to write this code, but I would love to see the same thinking in people when I ask a question like that. As a good developer, I am lazy. I create code that will be ready for future requests as well as the immediate problem. Or is that too much?
Do we need developers that think ahead?
Which brings me to the problem interview questions like these hint at. My approach comes from years of frustration of changing requirements. And I almost never got enough time to clean up my solutions. That’s why I padded my estimates so I have time to code what’s needed, and not what brings the immediate result. I plan on building products that last and change over time, with a solid, working core. But is this still even a thing our market wants?
Is software a disposable product?
Over the recent years, software has become a disposable product. Instead of software solutions, we built apps. Packaged and released solutions controlled by the software publisher. New features or fixes required a re-publication and upgrade by the end user. That was not what the web was about. Web products get new features without a need to upgrade, uninstall or re-install. The problem with that is that I can’t control the user as much as publishers want to, so we went the way of apps. We made the web a distribution platform for things our end users have no control over.
Nowadays apps are disposable. You can have them automatically generated by frameworks or even using AI and a prompt. Many tools allow you to create a design and generate code that makes the app work. But, the code makes this snapshot app work. It is not code that functions beyond the visual. And it is code not fit to take care of changes that might happen in the nearer future. Changes that will always come as user needs and environments change over time. I don’t know about you, but this all feels incredibly wasteful to me. Programming, to me, is about flexibility and reaction to change. Not to create a product akin to a physical thing I use and discard once its not serving the original purpose any longer.
Maybe my way of thinking is outdated, and not necessary any more and I put too much stake into creating generic, expandable code. But, as developers we shouldn’t be surprised that AI and code generation can take our jobs if all we do is “build this thing” and not “find a solution to this problem”.