InterviewBit | Abhishek Agrawal 's gists solutions | Hashing library
kandi X-RAY | InterviewBit Summary
kandi X-RAY | InterviewBit Summary
This repository is a collection of my gists (working :white_check_mark: code snippets passing all test cases on the InterviewBit online judge) solutions in the absolutely fantastic language, C++.
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 InterviewBit
InterviewBit Key Features
InterviewBit Examples and Code Snippets
void merge(int[] Arr, int start, int mid, int end) {
// create a temp array
int[] temp = new int[end - start + 1];
// crawlers for both intervals and for temp
int i = start, j = mid + 1, k = 0;
// traverse b
public int longestPalindrome(String s) {
int result = 0;
int len = s.length();
HashMap map = new HashMap<>();
for (int i = 0; i < len; i++) {
char ch = s.charAt(i);
map.put(ch, map.getO
public int solution(int[] A) {
double minAvg = Integer.MAX_VALUE;
int index = 0;
if (A.length <= 2) {
return 0;
}
for (int i = 0; i < A.length - 2; i++) {
if ((A[i] + A[i + 1]) /
Community Discussions
Trending Discussions on InterviewBit
QUESTION
Question is Count Total Set Bits: https://www.interviewbit.com/problems/count-total-set-bits/
My solution is:
...ANSWER
Answered 2020-Dec-12 at 07:20The problem in your code is here:
QUESTION
Question taken up from this website: https://www.interviewbit.com/problems/best-time-to-buy-and-sell-stocks-iii/
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most two transactions.
Note: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
My Solution:
...ANSWER
Answered 2019-Mar-12 at 15:43Local Minimums and maximum might not be optimal because we have 2 transactions available.
Your code sometimes uses wrong local minimums/maximums
For example given input:
1 1 3 1 3 2 4
Your solution gives: 4
Correct asnwer is: 5
QUESTION
The following is the link to the problem I'm trying.
https://www.interviewbit.com/problems/compare-version-numbers/
I simulated the array to compare both the versions. But I couldn't find any mistake in the code.
...ANSWER
Answered 2019-Sep-17 at 09:37You have an overflow. 4444371174137455
doesn't fit in int
. Try to use uint64_t
for vnum1
and vnum2
QUESTION
This is an interviewbit.com problem : https://www.interviewbit.com/problems/largest-distance-between-nodes-of-a-tree/
Given an arbitrary unweighted rooted tree which consists of N (2 <= N <= 40000) nodes. The goal of the problem is to find largest distance between two nodes in a tree. Distance between two nodes is a number of edges on a path between the nodes (there will be a unique path between any pair of nodes since it is a tree). The nodes will be numbered 0 through N - 1.
I am finding a node which is farthest from root node using dfs. From this node i am doing DFS to find the farthest node. this distance is the required answer. I implemented it, but while calling do_dfs function i am getting segmentation fault. i wrote return statement after every line to find out where i am getting error. I have indicated that line in comment in code.
...ANSWER
Answered 2019-Aug-30 at 06:24Change the line in Solution::solve(vector &A)
:
QUESTION
I am solving a problem on InterviewBit (Link) : https://www.interviewbit.com/problems/merge-k-sorted-lists/ I have to merge k sorted linked lists and return it as one sorted list. This is my solution:
...ANSWER
Answered 2019-Aug-03 at 19:50The problem with your code is that the list you are returning consists of pointers to nodes which have been destroyed. So your code has undefined behaviour.
QUESTION
I was solving a competitive programming problem on interviewbit.com I basically used a unordered_map to keep track of visited numbers. When I used operator[], my code could not perform in time, but it passes all tests when I used find. Both should have same time complexity.
I tried timing both codes using clock() by running them 10 times and averaging out the run times and they both gave more or less same time. I used g++ 7.4.0 while the environment provided by website has g++ 4.8.4. Could this be the reason for this.
...ANSWER
Answered 2019-Jul-31 at 05:08There are two reasons why the []-operator will be slower than find:
- The []-operator calls a non-const function on the map properly preventing all kinds of optimizations, like loop unrolling.
- The more import reason: The []-operator creates non-existing elements in the map with their default value. The map will be bloated with all pairs of A[i] + A[j], that were not previously in the map and set their values to 0. This will increase the map size and thus the time.
I think your performance measurements showed no difference between the two alternatives because of one or more of this reasons:
- The input vector is too small to make a difference
- Most combinations of A[i] + A[j] are already in the vector, so the unordered_map is not bloated enough to make a difference
- You did not optimize your code (-O3 or -Os) your code
QUESTION
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path 1->2->3 which represents the number 123.
Find the total sum of all root-to-leaf numbers % 1003.
example:
if 1 is root, its left child is 2 and right child is 3 then,
The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.
Return the sum = (12 + 13) % 1003 = 25 % 1003 = 25.
original problem is here
P.S: this is not homework, I'm preparing for college placements.
my attempt:
ANSWER
Answered 2019-May-21 at 13:21The line
if(!root->left) DFS(root->left, temp, ans);
should be
if(root->left) DFS(root->left, temp, ans);
Same thing for the right node. Basically, you never go down in the tree if a node exists.
Alternatively, you can simplify the code:
- Use integers instead of strings to make the computations lighter.
- Pass
temp
variable by copy, then you won't have to "pop_back" the last digit. - Call
DFS
without checking if the pointer is null since it already checks at the beginning. - Remove the last modulo operation since it was already done in
DFS
.
QUESTION
I was solving a DP question involving Catalan Numbers using JavaScript. I was getting the wrong answer for n = 35 (35th Catalan Number). So I decided to code in C++. I wrote similar code in C++ and to my surprise my code passed all test cases. Why am I getting different result for similar code in C++ and JS?
Problem Link : https://www.interviewbit.com/problems/intersecting-chords-in-a-circle/
JS Code(Rhino 1.7.7)
...ANSWER
Answered 2019-Jul-16 at 18:24That's because Javascript uses 64-bit IEEE 754 floating point numbers. If you change your C++ code to use the equivalent type (double), you get the same wrong result:
QUESTION
I am attempting the following question from Interviewbit:
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
NOTE: You can only move either down or right at any point in time.
I have written the following memoized solution:
...ANSWER
Answered 2019-Jul-09 at 16:25The test cases have negative numbers in the grid ( though they have explicitly mentioned non-negative numbers). So dp[i][j] can be negative but your function will never consider those values. Just used another vector to store the visited cell and it got accepted.
QUESTION
I always get this error whenever using input() in Python3 in any online compiler, hackerrank, wipro portal, interviewbit etc. I've seen so many posts regarding this, but none of them is working for me. try except block leads to always execution of the except block which I don't want as I'm still not able to read any input. Even as simple as the following code doesn't work. Help.
...ANSWER
Answered 2019-Jan-07 at 00:39Try going to https://www.hackerrank.com/challenges/python-loops/problem where the single input line is already there in the starter code for you. If you select Python 3 from the language dropdown, and then -- without entering any code of your own at all -- click Run Code, you should get a Wrong Answer "no response on stdout" response. Do you get that, or do you still get an EOFError? I'm assuming you don't get the EOFError and that perhaps there's a problem with where/how you're entering your code into their editor.
If you're getting an error like that with InterviewBit, I'd say it's because with InterviewBit you're not supposed to be reading from stdin at all -- the starter code will have a function that their test cases call, and then you complete the function code to return the desired output.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install InterviewBit
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