Day 27 — Baseball Game. 100 Days to Amazon
By: House Of Codes
Tags: Programming, Coding, Software Development, Interview, Java

Day 27 — Baseball Game

100 Days to Amazon

100 Days to Amazon — Day 27 — Baseball Game

Photo by Jose Morales on Unsplash

Out of Free Stories? Here is my Friend Link.

Introduction

Hey Guys, Today is day 27 of the challenge that I took. Wherein I will be solving every day for 100 days the programming questions that have been asked in previous interviews.

You have a bonus at the end if you keep reading. You can find out the companies that have asked these questions in real interviews.

All these problems are taken from the following e-book. 🎓

This is completely free 🆓 if you have an amazon kindle subscription.

This e-book contains 100 coding problems that have been asked in top tech interview questions. It also has a guide to solving all the problems in 200+ ways. These problems I assure you has been asked in previous interviews.

You have to decide whether you want to go unprepared for a tech interview or go ahead and quick search for this guide to solve the 100 problems.

Begin Your ascent to greatness

Day 27 — Baseball Game

AIM🏹

You’re now a baseball game point recorder.

Given a list of strings, each string can be one of the 4 following types:

Integer (one round's score): Directly represents the number of points you get in this round. "+" (one round's score): Represents that the points you get in this round are the sum of the last two valid round's points. "D" (one round's score): Represents that the points you get in this round are the doubled data of the last valid round's points. "C" (an operation, which isn't a round's score): Represents the last valid round's points you get were invalid and should be removed.

Each round’s operation is permanent and could have an impact on the round before and the round after.

You need to return the sum of the points you could get in all the rounds.

Example

Input: ["5","2","C","D","+"]

Output: 30

Explanation:

Round 1: You could get 5 points. The sum is: 5.

Round 2: You could get 2 points. The sum is: 7.

Operation 1: The round 2's data was invalid. The sum is: 5.

Round 3: You could get 10 points (the round 2's data has been removed). The sum is: 15.

Round 4: You could get 5 + 10 = 15 points. The sum is: 30.

Code👇

Author: Akshay Ravindran

Algorithm👨‍🎓

Create a stack that will store the values after each operation. If the value is C. It is just a pop() operation. If the value is an integer we have to push that value into the Stack. If the operation is D. We will get the top value of the stack and multiply it by two and push it into the stack. If the operation is +. Remove the top element add the current top with the previous top element. Push this new value onto the Stack. When you reach the end of the array, Traverse through the stack. Adding each value of the stack. Return the total 🔚

Conclusion

Have you come across this question in your interview before? Share it in the comment section below. 🤝

Don’t forget to hit the follow button✅to receive updates when we post new coding challenges. Tell us how you solved this problem. 🔥 We would be thrilled to read them. ❤ We can feature your method in one of the blog posts.

Want to become outstanding in java programming?

Click HERE 🧨🎊🎃

I have published an ebook. A compilation of 100 Java(Interview) Programming problems which have been solved. (HackerRank) 🐱‍💻

This is completely free 🆓 if you have an amazon kindle subscription.

Companies

Previous Blog Posts