hackerrank | various languages from the site Hacker Rank | Learning library
kandi X-RAY | hackerrank Summary
kandi X-RAY | hackerrank Summary
This repository holds solutions in various languages for miscellaneous problems from HackerRank. Feel free to open PRs to add more solutions and/or solutions for different languages. However, PRs with code that isn't commented sufficiently for others to easily understand the algorithms and/or data structures will be rejected.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of hackerrank
hackerrank Key Features
hackerrank Examples and Code Snippets
Community Discussions
Trending Discussions on hackerrank
QUESTION
I am solving this problem on dynamic array in which input first line contains two space-separated integers,n, the size of arr to create, and q, the number of queries, respectively.
Each of the q subsequent lines contains a query string,queries[i]
. it expects to return int[]
: the results of each type 2 query in the order they are presented.
i tried to attempt as below and my code seems fine to me but it gives segmentation fault error. please help me where I am getting conceptually wrong. thanks.
problem: Declare a 2-dimensional array,arr
, of n
empty arrays. All arrays are zero indexed.
Declare an integer,last answer , and initialize it to zero
.
There are 2
types of queries
, given as an array of strings for you to parse:
Query: 1 x y
Let idx=((queries[i][1]^last_answer)%n);
.
Append the integer y to arr[idx]
.
Query: 2 x y
Let idx=((queries[i][1]^last_answer)%n);
.
Assign last_answer=arr[idx][queries[i][2]%(arr[idx].size())]
.
Store the new value of last_answer
to an answers array.
input: 2 5
1 0 5
1 1 7
1 0 3
2 1 0
2 1 1
output:
7
3
...ANSWER
Answered 2021-Jun-15 at 11:25You are accessing elements of vector
without allocating them.
resize()
is useful to allocate elements.
QUESTION
So I have a hackerRank different type of fizzBuzz function that is as follows:
The only constraints:
0 < n < 2 x 10^5.
But for simplicity, I am not using the constraints here so the answer won't be so big. Here it is:
if n is multiple by both 5 and 3 print('FizzBuzz')
if n is multiple by 3(but not 5) print('Fizz')
if n is multiple by 5(but not 3) print('Buzz')
if n is neither print(n)
The prints should be one value per line
So far ok, here is the code I wrote in Python 3
...ANSWER
Answered 2021-Jun-13 at 15:20As you can see this is the same evaluation as your code but with optimized approach, FizzBuzz questions are to test on how to write an efficient algorithm. Just because the code is working it doesn't mean it shouldn't be optimized.
In here you can see I'm checking every interval and appending the string by words. If you use modulo(%) operator, then it uses more cpu clocks, I know it is nothing for current generation computers, but it's good practice with problems like this
QUESTION
I've begun learning Go, and I ran into a strange bug, and an even stranger fix for it working on a HackerRack problem:
...ANSWER
Answered 2021-Jun-12 at 06:41So we have an actually correct answer here, the issue is that you're writing to the boolean but never reading from it. Without the Println()
, it's not used in a conditional or any other expression anywhere that depends on its value, so the assignments to it don't affect the program flow. You could remove all of the lines assigning values to insideValley
and the program would act no differently than it does right now (excepting the Println(), of course, which is why adding that "fixed" the issue).
Go is specifically designed to flag "junk" code like that, that adds nothing to the program flow, as a compiler error (well, in most cases. Unused globals and unused functions are some exceptions to that). Simply add in whatever is supposed to be using that boolean's value (such as a conditional based on its value), and you'll stop getting the "variable unused" error.
And as noted in the comments of Vishwa Ratna's answer, vars do not have to be used in every logical pathway. They only need to be used (ie. read from) in at least one logical pathway.
QUESTION
What sites help me to increase my hacking skills, I'm a newbie, for coding there are hackerrank, hackerearth, codeforces, codechef etc, like those what are there to learn hacking?
...ANSWER
Answered 2021-Jun-11 at 09:01I would say CTF challenges are a good way to learn. https://tryhackme.com/ is a good place to start. Most of the rooms are aimed at beginners and it will give you a good understanding, of some of the programs you will use like nmap, burpsuite and wireshark. https://www.hackthebox.eu/ is a good place, when you are a bit more experienced.
I am also gonna share some GitHub links, you might find interesting:
QUESTION
Let's say that you have a string:
...ANSWER
Answered 2021-Jun-10 at 00:55The behavior for Java is documented in Pattern:
In this class, \1 through \9 are always interpreted as back references, and a larger number is accepted as a back reference if at least that many subexpressions exist at that point in the regular expression, otherwise the parser will drop digits until the number is smaller or equal to the existing number of groups or it is one digit.
QUESTION
Recently came upon a question about Bars and Stars. I can't remember it in its entirety but I got it wrong and I'm breaking my head trying to figure out exactly where I went wrong.
The question is this one https://www.chegg.com/homework-help/questions-and-answers/given-string-stars-bars-determine-number-stars-two-bars-within-substring-star-represented--q38765107
My code was pretty straightforward.
...ANSWER
Answered 2021-Jun-06 at 02:40The only problem I see with your code is s.slice(start_i - 1, end_i)
. That is problematic when start_i = 0
and end_i = s.size - 1
. For example, "abc".slice(0-1, 2) #=> "2"
, because -1
and 2
both index the last character of the string.
Note you can replace reduce(&:+) || 0
with reduce(0, &:+)
, or, better, sum
.
I do think substring.gsub!('|', '||')
is a bit hokey. You could instead write
QUESTION
I was doing this question on HackerRank (Diagonal Difference) and I am currently facing an issue.
Here is the link to the question:
https://www.hackerrank.com/challenges/diagonal-difference/problem
The problem is that I'm constantly getting an error: int cannot be converted to int[][]
.
I tried initialising the variable j but that too is not solving the issue.
Here is my code:
...ANSWER
Answered 2021-Jun-03 at 08:55The first argument to diagonalDiff
method is an array (2-D). But you're passing an int
while calling it. (a[i][j]
will be an int
)
try calling it this way:
QUESTION
i'm currently trying to solve a HackerRank problem, the problem in question is called Fibonacci Modified.
The method returns an int but it is expected that I will be obtaining huge values. I'm solving this problem in Java.
Here is my code
...ANSWER
Answered 2021-May-31 at 01:26You cannot, the maximum value for int is 2,147,483,647
(32 bits of value). If you need big numbers you must use the appropriate variable type.
If for some reason you want to avoid BigInteger
at all and your are not going to do any arithmetic operation later, you can always return a String
About the hackerrank problem, just modify the result variable type to BigInteger
. Actually, they seem to be aware of the 32/64 bits issue... they are using a String
value to prevent it.
There's no reason to keep the whole code-template structure. They just care about input/output. You are allowed to modify EVERYTHING but those two things.
This is their output (here you can see their output is expected to be a String
):
QUESTION
So I designed this code (given below) for the question & it seems to be giving all the answers there are but for some reason I'm not able to pass my test cases in the Hackerrank Question except for the sample one.
My Code :
...ANSWER
Answered 2021-May-29 at 00:12You have three problems with this code:
- An actual bug. You don't handle when a prime divides the number multiple times. I'm fairly sure the four lines before the
println
output are a workaround you put in when you encountered a failure this causes, that fixed the specific failure you found but don't actually address the root cause. I just tested it, your code outputs 3 for the input 252. The correct output for 252 is 7. - Part of your code invokes a library method that may be significantly expensive. You should not be using
isProbablePrime
. - Your algorithm's logic scales poorly. Hackerrank's test cases probably include something where the correct output is something like 20 digits long. A major point of Project Euler is that it's not enough to write code that will find the correct answer. You need to write code that will find the correct answer efficiently. To fix this, you will need new logic for the fundamental design of the program.
QUESTION
I'm doing this question on hackerrank: https://www.hackerrank.com/challenges/ctci-bubble-sort/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=sorting
I wrote the solution in intellij, and it gives me the correct output there, but when I copied it over to the hackerrank ide, it gave me an error.
This is the code I'm talking about:
...ANSWER
Answered 2021-May-27 at 16:04You have put the Solution class within your Result class. HackerRank wants you to put the Solution class as its own class, like this:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install hackerrank
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page