Amazon online assessment - my results

I had two online assessments, which consisted of 2 coding questions, the 2nd assessment had one of the same questions as the first assessment. In the first one, I passed 19/25 test cases for
each question, so it was a fail. Here are all the questions, and my code. Can anyone fix it so they can pass 25/25 test cases?

  1. In-Flight Media: You are a in-flight movie service provider. You are given a list of movie lengths and the duration of the flight. Passengers watch exactly 2 movies, and no movie is returned twice. Return a pair of (2) movie Id's that finish exactly 30 minutes before the plane lands. If multiple such combinations are possible, return the pair which has the movie of longest duration. If no such pair is possible, return <-1,-1>.

Here is what I wrote (C#): (passed 19/25 test cases, but ran out of time to fix it):

        internal List<int> foo(int flightDuration, List<int> movieDuration)
        {
            var sortedlist = new List<int>();
            sortedlist.AddRange(movieDuration);
            sortedlist.Sort();

            var i = 0;
            var j = movieDuration.Count - 1;
            var max = -1;

            var movie1len = 0;
            var movie2len = 0;
            var maxlen = 0;

            while (i < j)
            {
                var sum = sortedlist[i] + sortedlist[j];
                if (sum == flightDuration - 30)
                {
                    if (sortedlist[i] > maxlen || sortedlist[j] > maxlen)
                    {
                        movie1len = sortedlist[i];
                        movie2len = sortedlist[j];
                        max = sum;
                        maxlen = Math.Max(movie1len, movie2len);
                    }
                    i++;
                    j--;
                }
                else if (sum < (flightDuration - 30))
                    i++;
                else
                    j--;
            }

            var mtw = new List<int>();
            if (max == -1)
            {
                mtw.AddRange(new int[] { -1, -1 });
            }
            else
                mtw.AddRange(new int[] { movieDuration.IndexOf(movie1len), movieDuration.IndexOf(movie2len) });
            mtw.Sort();
            return mtw;
        }
	```
	
	2) Robot Rodeo:
	On an infinite plane, a robot initially stands at (0, 0) and faces north. The robot can receive one of three instructions:

    "G": go straight 1 unit;
    "L": turn 90 degrees to the left;
    "R": turn 90 degrees to the right.

The robot performs the instructions given in order, and repeats them forever.

Return(List<string>) "YES" or "NO" (for each command in List<string> commands) if and only if there exists a circle in the plane such that the robot never leaves the circle:

(Similar to https://leetcode.com/problems/robot-bounded-in-circle/  but need to return a list as there are multiple sets of commands)

My code which passed 19/25 test cases:


        private static readonly (int dx, int dy)[] directions = { (0, 1), (1, 0), (0, -1), (-1, 0) };
        internal List<string> doesCircleExist(List<string> commands)
        {
            var initiald = 0;
            var diridx = 0;

            var bwc = new List<string>();

           
            foreach (var command in commands)
            {

                (int x, int y) position = (0, 0);
                foreach (var c in command)
                {



                    switch (c)
                    {
                        case 'G':
                          
                            position.x += directions[diridx].dx;
                            position.y += directions[diridx].dy;
                            break;
                        case 'L':
                          
                            diridx = (diridx + directions.Length - 1) % directions.Length;
                            break;
                        case 'R':
                          
                            diridx = (diridx + 1) % directions.Length;
                            break;
                        default:
                            break;

                    }

                }
                if (position.x == 0 && position.y == 0)
                    bwc.Add("YES");
                else
                    bwc.Add("NO");

            }

          


            return bwc;

        }
3) Cloudfront Caching:
To Account for the increase in efficiency as more nodes are connected, update the cost of each isolated set to be the ceiling of the square root of the original cost and return the final sum of all costs:  
example:   n = 10 nodes
edges = [[1 2],[1 3],[2 4],[3 5],[7 8]]
2 isolated sets (1 2 3 4 5) and (7 8)
ceilings of sqrt are 2.236 == 3, and 1.414 = 2.
so answer is 3 + 2 + 3 = 8

Heres my code that passed about 3 test cases, LOL:

        internal int connectedSum(int n, List<string> edges)
        {
            var iedges = new List<List<int>>();
            foreach (var edge in edges)
            {
                var nums = edge.Split(' ').Select(x => int.Parse(x)).ToList();
                iedges.Add(nums);

            }

            var counts = new List<int>();
            var sqrts = new List<double>();
            var count = 0;
            bool inlist = false;
            var alledges = new List<int>();
            var dict = new Dictionary<int, int>();
            for (var i = 0; i < iedges.Count; i++)
            {
                if (inlist && !dict.ContainsKey(iedges[i][0]) && !dict.ContainsKey(iedges[i][1]))
                {
                    counts.Add(count);
                    count = 0;
                    inlist = false;
                    foreach (var elem in dict)
                    {
                        alledges.Add(elem.Key);
                    }
                    dict.Clear();
                }
                if (!inlist && !dict.ContainsKey(iedges[i][0]) || !dict.ContainsKey(iedges[i][1]))
                {


                    if (!dict.ContainsKey(iedges[i][0]))
                    { 
                        dict.Add(iedges[i][0], 1);
                        count++;
                    }

                  

                    if (!dict.ContainsKey(iedges[i][1]))
                    {
                        dict.Add(iedges[i][1], 1);
                        count++;
                    }
                    inlist = true;

                }
               

                
            }


            foreach (var elem in dict)
            {
                if (!alledges.Contains(elem.Key))
                    alledges.Add(elem.Key);
            }

            if (count > 0)

            {
                counts.Add(count);
            }

            var totals = 0;
            foreach (var c in counts)
            {
                var sqrt = (int)Math.Ceiling(Math.Sqrt(c));
                totals += sqrt;
            }

            var remsqrts = 0;

            for (var i = 1; i <= n; i++ )
            {
                if (!alledges.Contains(i))
                    remsqrts++;
            }

            return totals + remsqrts;
            
        }

Could anyone fix the code, or point me towards a solution? I still need practice before I attempt this again!

Thanks!!

Comments (5)