
Lessons learned from “saving the princess”
How I got out of my comfort zone, failed gracefully, and won the day learning about AI!
*Disclaimer — the following write up is intended to be an after-action report on what I learned from a hackerrank challenge as I prepared for an interview and not a complete dissertation on AI, LLMs, and the like.
Background
I wish to preface this post by stating that I am not a coder. I have no formal education in Computer Science, Data Structures & Algorithms, or AI / ML.
The extent to which I know how to code is derived from a situation I found myself in: phased out of a job I truly enjoyed. I was a manual tester and there was an organizational need for software developers in test with experience in Java and Objective-C (later Swift). I knew neither of these and so I was unceremoniously cast out. It was the push I needed.
Rather than sulk in my misery, I recognized where I was deficient and set goals to improve this weakness. When I wasn’t looking for work, I spent my down time learning Java, then python, and eventually javascript.
To this day, I’d like to think I’m somewhat competent in at least two of those three, but can adapt as needed.
Enter AI Testing Opportunity
An acquaintance I see from time to time referred me to this exciting opportunity to work as a freelancer in the role of optimizing prompts for improved output. The extent of what this job entails remains to be determined considering I just completed the online application process and the technical assessment. Its job with flexible time commitment and an opportunity to make some side money working with LLMs. Win-Win, right?!
The “onboarding” process consists of a series of tasks they assign you. Your first task is to complete your profile. The second task is to schedule the technical interview. There is only one round in the interview process, so not much else can be said about that.
When you click on the task button on your dashboard to schedule your appointment, you can choose any day to you wish via Calendly. I opted to give myself enough time to prepare. Knowing it was a live coding challenge made me a bit terse since I hadn’t spoken to anyone about the role I was getting into, the particulars of the job, and so on. I get stage fright with live coding challenges so this was not going to be fun. But I set my bias aside and started cramming for the test.
Because this is a job concerning working with prompts and generative models, I went to hackerrank and found this section of challenges working with AI. A couple of things I should point out:
- I haaate the use of hackerrank / leet code for QA interviews (for reasons I won’t get into here)
- Although labeled EASY, given my aforementioned lack of proper education in CS, these challenges were far from that.
The Challenge
The princess is trapped in one of the four corners of a square grid. You are in the center of the grid and can move one step at a time in any of the four directions. Can you rescue the princess?
I should point out that I’ve made it a point this year to overcome my bias for hackerranck and leet code challenges and actually learn how they work and get past my own “stage fright” during interviews. So I blocked out time and went over to the hackerrank site — AI module — and proceeded to try their EASY challenge. I was not expecting to have as much fun as I did.
- You get to pick the language you want, which is awesome. I went with python, as it happens to be where I want to improve the most, and what I’m familiar with.
- Understanding the question is truly the most important step of solving the problem. Where I have failed with these challenges in the past is impulsively jumping to the solution without thinking through the problem.
- Speaking for myself, the hardest part of live coding challenges is getting out of your head and speaking out your thought process to the interviewer. Not something I do regularly.
- There is always an optimal way to solve the problem. Like everything else in my life, I did it the hard way.
The Task
Complete the function displayPathtoPrincess() which takes in two parameters the integer N and the character array grid. The goal is to reach the princess in as few moves as possible.
I won’t go into the particulars of what all the rules of the game are. I’ve provided the link earlier in this post, and down below. I will say that this taught me a lot more about how artificial general intelligence works than I expected. The super-simplest break down is as follows:

- The grid parameters need to be determined. This will set the stage for the location of the princess and where the hero (bot) is in relation. For simplicity’s sake, the bot starts at the center of the grid.
- The grid coordinates are the second step in the sequence, occurring as a set of (x,y) pairs. Knowing the coordinates helps determine what shortest the path to take should be (bot movement).
- Once these values are calculated (location of princess, location of bot), the rest is a matter of iterating through the grid to compare where the princess is located in relation to where the hero is. Simply put: the input determines the output.
- The output of the calculated location is move — the path taken.
The Script
Here’s what the solution looks like in python, employing list comprehension for efficiency and legibility (because mine looked way worse!)
def path_to_princess(grid):
z = len(grid) - 1
corners = [
(0, 0), (0, z),
(z, 0), (z, z)
]
p = next((r, c) for r, c in corners if grid[r][c] == 'p')
m = (z // 2, z // 2)
row_diff, col_diff = p[0] - m[0], p[1] - m[1]
yield from (['UP'] * -row_diff if row_diff < 0 else ['DOWN'] * row_diff)
yield from (['LEFT'] * -col_diff if col_diff < 0 else ['RIGHT'] * col_diff)
n = int(input())
grid = [input().strip() for _ in range(n)]
for move in path_to_princess(grid):
print(move)
The Output
Lesson(s) Learned
This was by far one of the most challenging (and the most fun) hackerrank problems I had ever come across. It took me out of my comfort zone and exposed me to new ideas I had never considered. Some lessons learned:
- I have never worked with an algorithm like this, so saying it was an uphill climb is quite the understatement.
- I am far more inexperienced with python than I previously thought. My solution was longer and harder to read, and it didn’t work. The solution I researched was clean and used yield from() which I had never worked with before.
- I got a deeper look under the hood to see just how a language model responds to the prompt you give it by running through a supremely complex computational algorithm to arrive at an approximate answer (in seconds) to your prompt. The magic is in the complexity of this algorithm that is continuously improving.
And there you have it! I failed the challenge in spectacular fashion and learned a lot. I stepped out of my comfort zone and approached this problem with glorious purpose. For me, the win wasn’t in the solution (which I had to google) but rather in the learning and doing. I hate hackerrank a little less now.
Of course, I bombed the interview. The interviewer asked the question I was not prepared for, and completely removed from anything resembling AI or what my job is.
Didn’t matter. I won the day, overcame my insecurity, and rescued the princess.
[Update 3/15/2024 07:00 EST] I got accepted. On to the next chapter and what that might bring!
- https://www.hackerrank.com/domains/ai
- Different challenge with expanded write up on LLM: https://python.plainenglish.io/breaking-ai-hackerrank-challenges-bot-clean-1ec7f7204fca
- Optimal solution to the “bot saves princess challenge”:
user 200_success: https://codereview.stackexchange.com/a/131626