Minesweeper field marking
Interview Notices

Check the result of system calls

FILE* input = fopen(file, "r");      // Can return null
fread(temp, 1, sizeof temp, input);  // returns the number read (which may not be sizeof temp
fclose(input);                       // Can fail


Style

Personally don't like:

' ' != temp[i] &amp;&amp; '\n' != temp[i]


Reads way too much like Yoda conditionals (which went out of style ten years ago). Its not the natural way to expresses a test in your brain.

temp[i] != ' ' &amp;&amp; temp[i] != '\n'


Reads much more naturally (at least to an English speaker). Though in an interview I would not knock points off for this. If I hired you I would re-train you to do it the other way though.

Is this the correct/good interface?

void read_matrix(char* dest, char const* file)


I don't like that dest is an array of char. Are you optimizing for space? Where you asked to optimize for space? I would change that to an array of int, as that expresses intent much more clearly.

void read_matrix(int* dest, char const* file)


I think manually checking for space is making this complex. I would have used the scanf library function to do that work for me.

  for(int i = 0; i &lt; 128; ++i){
    if(' ' != temp[i] &amp;&amp; '\n' != temp[i]){
       dest[d++] = temp[i];
    }
 }


// note: I would have made the `dest` array integers
for(int i = 0; i &lt; 128; ++i){
    if (sscanf(temp, " %d", dest + i) != 1) {
        // ERROR
    }
}


Functions beginning with an underscore and a lowercase letter are reserved in the global scope.

char _grid_at(char const* grid, int x, int y)
int  _surround_count(char const* grid, int x, int y)
//   ^^^ reserved identifier.


Don't change the first letter to upper case as an underscore followed by an upper case letter is reserved in all contexts.

I can see why you had an issue (all those tests).

A simple optimization for this is to place your grid inside a grid that is one square larger vertically and horizontally. So if you have an 8×8 grid, place it inside a 10×10 grid. Then mark all the cells around the side as having zero in them.

Then when you call surround_count() you can add the cost of all the squares around the cell:

int surround_count(char const* grid, int x, int y)
{
     assert ( 1 &lt;= x &amp;&amp; x &lt;= 8 &amp;&amp; 1 &lt;= y &amp;&amp; y &lt;= 8);

     int count = 0;
     for(int h = -1; h &lt;= 1; ++h) {
         for(int v = -1; v &lt;= 1; ++v) {
             count += isBombAt(grid, x + h, y + v);
         }
     }
     return 0;
 }