Skip to content

Instantly share code, notes, and snippets.

@ricanteja
Created March 3, 2021 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ricanteja/59500e74debc7fa68a59ec73a0321b2e to your computer and use it in GitHub Desktop.
Save ricanteja/59500e74debc7fa68a59ec73a0321b2e to your computer and use it in GitHub Desktop.
// Ricardo Tejada (c) 2021
using System;
using System.Collections.Generic;
namespace InterviewQuestions
{
class Program
{
static void Main(string[] args)
{
var Blocks = new List<Dictionary<string, bool>>()
{
new Dictionary<string, bool>()
{
{ "gym", false }, { "school", true }, { "store", false }
},
new Dictionary<string, bool>()
{
{ "gym", true }, { "school", false }, { "store", false }
},
new Dictionary<string, bool>()
{
{ "gym", true }, { "school", true }, { "store", false }
},
new Dictionary<string, bool>()
{
{ "gym", false }, { "school", true }, { "store", false }
},
new Dictionary<string, bool>()
{
{ "gym", false }, { "school", true }, { "store", true }
}
};
var Reqs = new string[] { "gym", "school", "store" };
int lastScore = 0, highScore = 0, bestBlock = 0;
for (int i = 0; i < Blocks.Count; i++)
{
int currScore = 0;
for (int j = 0; j < Reqs.Length; j++)
currScore = (currScore << 1) | (Blocks[i][Reqs[j]] ? 1 : 0);
if ((currScore | lastScore) >= highScore)
{
bestBlock = i;
highScore = currScore | lastScore;
}
lastScore = currScore;
}
Console.WriteLine($"Best block to live on is #{bestBlock}");
}
}
}
@Kevin-Andrew
Copy link

@ricanteja

If we expand the input data, in order to allow for more testing combinations, your code doesn't appear to work. I added one more location item type, park, as well as several more blocks. Given the following, the answer should be block index 5. But your code is still outputting 3.

using System;
using System.Collections.Generic;

namespace InterviewQuestions
{
    class Program
    {
        static void Main(string[] args)
        {
            var Blocks = new List<Dictionary<string, bool>>()
            {
                new Dictionary<string, bool>()
                {
                    { "gym", false }, { "school", true }, { "store", false }, { "park", false } // #0
                },
                new Dictionary<string, bool>()
                {
                    { "gym", true }, { "school", false }, { "store", false }, { "park", false } // #1
                },
                new Dictionary<string, bool>()
                {
                    { "gym", true }, { "school", true }, { "store", false }, { "park", false } // #2
                },
                new Dictionary<string, bool>()
                {
                    { "gym", false }, { "school", true }, { "store", false }, { "park", false } // #3
                },
                new Dictionary<string, bool>()
                {
                    { "gym", false }, { "school", true }, { "store", true }, { "park", false } // #4
                },
                new Dictionary<string, bool>()
                {
                    { "gym", false }, { "school", true }, { "store", true }, { "park", false } // #5
                },
                new Dictionary<string, bool>()
                {
                    { "gym", false }, { "school", true }, { "store", true }, { "park", false } // #6
                },
                new Dictionary<string, bool>()
                {
                    { "gym", false }, { "school", true }, { "store", true }, { "park", false } // #7
                },
                new Dictionary<string, bool>()
                {
                    { "gym", false }, { "school", true }, { "store", true }, { "park", true } // #8
                },
            };

            var Reqs = new string[] { "gym", "school", "store", "park" };
            int lastScore = 0, highScore = 0, bestBlock = 0;

            for (int i = 0; i < Blocks.Count; i++)
            {
                int currScore = 0;

                for (int j = 0; j < Reqs.Length; j++)
                    currScore = (currScore << 1) | (Blocks[i][Reqs[j]] ? 1 : 0);

                if ((currScore | lastScore) >= highScore)
                {
                    bestBlock = i;
                    highScore = currScore | lastScore;
                }

                lastScore = currScore;
            }

            Console.WriteLine($"Best block to live on is #{bestBlock}");
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment