CtCI | Java solutions to Cracking the Coding Interviews | Learning library
kandi X-RAY | CtCI Summary
kandi X-RAY | CtCI Summary
Java solutions to Cracking the Coding Interviews (6th Edition).
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Creates a soft - linked list
- Print a list
- Append node to head
- Gets the integer value
- Create a singly - linked list
- Print a list
- Append node to head
- Gets the integer value
- Compresses the string and returns the compressed string
- Reverse a null terminated string
- Removes duplicates from list and adds them to the list
- Returns true if all the characters in the string are all unique
- Checks if two strings are permutation
- Returns true if all unique characters in the string are all unique
- Replaces all non - escaped whitespace characters in a string with plain text
- Checks if a string contains all unique chars
- Replace all whitespace characters in a string
CtCI Key Features
CtCI Examples and Code Snippets
Community Discussions
Trending Discussions on CtCI
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:
QUESTION
I have the following solution:
...ANSWER
Answered 2021-May-21 at 11:54after testing this for about an hour or so, I realized where you're mistaken. Namely, using prn
instead of print
prints out the quote characters alongside the actual text. This was a surprise to me, since I always thought that these two are interchangeable. If you change your prn
s to println
s, you should be okay.
The final code that I created which passed all of the tests:
QUESTION
We have a method in which an array and number of rotations are passed as input and we have to return the array after left rotations. Here is my solution.
...ANSWER
Answered 2020-Sep-09 at 14:51When you do the following:
QUESTION
https://www.hackerrank.com/challenges/ctci-bfs-shortest-reach/problem In this problem, I tried BFS by just (first approach) visiting the queue_front node and pushing its children into the queue and not visiting its children nodes instead of (second approach) visiting all its children and then pushing them into the queue. In the first approach, the test cases failed, but in the second approach, it passed. But in both the cases I am using BFS. So why is it failing then. The code is below.In this code first approach is enabled which is failing some test cases in the above hackerrank problem. The second approach passes all the test cases. Please look at the bfs function. I am using adjacency list to implement graph. PLEASE HELP!! THANK YOU!!
...ANSWER
Answered 2020-Aug-01 at 07:31Visiting nodes when you pop them out of the queue will be same as dfs it will not ensure you shortest path.if you will visit nodes then push it into the queue it will give you the shortest path consider the case :
node 1 is connected to node 2 and node 2 with node3 and node3 with node1 in a cyclic form
we will use a queue> q; first element of pair is the node and second element is distance to the node from start.
Case 1: this is a cyclic graph with start node as 1 we want to find min distance from 1 to 3.we push 1 into queue.we will pop it out and push node 2 and 3 into the queue without visiting it.when we will pop out 2 then we visit 2 and push 3 so when we pop out 3 we will check it the popped node is equal to 3 and we store the min distance.so in this case our min distance is 2.which we have find out using depth traversal.
But the answer should be 1 as 1 is directly connected with 3.
case 2:we will visit the node before pushing it into queue
start node is 1 and push it.visit 2 and 3 and then push both into stack.when we will pop node 2 we check the adjacent elements and both 3 and 2 are visited.so we will pop 3 and check if it is destination node.so we will store the distance.so in this case dist[1] to dist[3]=0+1 which is 1.
QUESTION
public class Common {
public static int getCharNumber(Character c) {
int a = Character.getNumericValue('a');
int z = Character.getNumericValue('z');
int val = Character.getNumericValue(c);
if (a <= val && val <= z) {
return val - a;
}
return -1;
}
public static int[] buildCharFrequencyTable(String phrase) {
int[] table = new int[Character.getNumericValue('z') - Character.getNumericValue('a') + 1];
for (char c : phrase.toCharArray()) {
int x = getCharNumber(c);
if (x != -1) {
table[x]++;
}
}
return table;
}
}
...ANSWER
Answered 2020-Jul-13 at 08:20Why is the getCharNumber case-insensitive?
The getCharNumber
method uses Java's Character#getNumericValue(char)
method for which its JavaDoc states in particular:
The letters A-Z in their uppercase ('\u0041' through '\u005A'), lowercase ('\u0061' through '\u007A'), and full width variant ('\uFF21' through '\uFF3A' and '\uFF41' through '\uFF5A') forms have numeric values from 10 through 35. This is independent of the Unicode specification, which does not assign numeric values to these char values.
Meaning that for example for character A
and a
this API method returns the same value, i.e. 10
, and thus there's no case-sensitivity.
For reference, see also
QUESTION
I am learning Python, and i use it to solve tasks on HackerRank. I have the problem with exercise Hash Tables: Ransom Note. I have written that code:
...ANSWER
Answered 2020-Jun-11 at 14:44You cannot referenze the varible your dict comp is assigned to inside itself - it does not update "iteratively".
It is easy to show:
QUESTION
I am going through the CTCI book and can't understand one of their examples. They start with:
...ANSWER
Answered 2020-May-01 at 08:24Unlike the time complexity, which is simply a total time that is needed to run a program, the space complexity describes the space required to execute the program. So it doesn't really matter that there are 2n nodes in the execution tree of the program. The call stack automatically folds and releases the additional memory used. What matters is the maximal depth of the call tree, which is O(n) for this program. Should be noted, though, that recursion is a special case that naturally releases any used memory upon stack fold. If memory is allocated explicitly during runtime, it should be released explicitly as well.
Regarding the first example, the call tree is simply a list of depth n, resulting in similar complexity of O(n).
QUESTION
In response to the Hackerank problem: https://www.hackerrank.com/challenges/ctci-making-anagrams/problem where one must find the number, as an integer, of characters to be removed from a string to make it an anagram of another string.
I have completed the code and the program passes the tests but I am wanting help with increasing its efficiency. How do I go about thinking how to improve the efficiency of the following code?
...ANSWER
Answered 2020-Apr-29 at 11:39First rule of optimization: Avoid unnecessary operations:
- Like calling to
contains
before callingadd
.
Second rule of optimization: If you want it faster, you'd better lean on memory:
- Do not call the same function several times with the same input values: Better call only once, store the value in a local variable, and use it afterwards.
- Also, computing the number of occurrences of a character in a string is not efficient (the longer the strings, the least efficient): Better create a map for each string, mapping each character to a number of occurrences.
- Dave's suggestion about how to optimize such maps is interesting, too.
QUESTION
I was looking at the code for how to remove duplicates from a list and I came upon some syntax I am unfamiliar with. What does $1 ++ $0 mean?
...ANSWER
Answered 2020-Apr-13 at 18:36$0
is the first parameter passed into the closure.
$1
is the second parameter.
++
is a custom infix operator
QUESTION
I have written a simple for loop meant to check if all of the words in one list are found in a larger list. It is a practice question found here.
I do not understand why my function does not execute properly - After running it, what is left in the sample list "note2" is ['one','today'], so it seems like the loop is somehow skipping over those words. Why is that?! I do not understand it conceptually.
Thank you for your help on this.
Example lists (two pairs of examples):
...ANSWER
Answered 2019-Nov-16 at 18:34In the loop you remove elements from the list and then word
is looking at the elements of the reduced list.
Copy the lists in the for
loop line using note[:]
:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install CtCI
You can use CtCI like any standard Java library. Please include the the jar files in your classpath. You can also use any IDE and you can run and debug the CtCI component as you would do with any other Java program. Best practice is to use a build tool that supports dependency management such as Maven or Gradle. For Maven installation, please refer maven.apache.org. For Gradle installation, please refer gradle.org .
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