Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!

Md Zuanyeed Kamal

Ranch Hand
+ Follow
since Dec 02, 2018
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
5
Received in last 30 days
0
Total given
26
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Md Zuanyeed Kamal

In order to know how many elements. I have to write another method like countTotal()   .
1 day ago

Campbell Ritchie wrote:If you submit something with a public field for a coding interview, you will fall at the first hurdle If that sort of code is typical of that book, it isn't worth reading, I am afraid.


you are right, I fell at the first hurdle, when it comes to understanding the way code was LinkedListNode class was mentioned in the book. Good news is: Now I can implement linkedlist by my own. I implemented a singly LinkedList that was very simple.
Below are the codes I implemented while learing linkedlist. My codes are very easy to follow. But, the way LinkedListNode was implemented in the book is giving me hardtime






3 days ago

Junilu Lacar wrote:Let's ignore the flaws of this design for a minute and just analyze it at face value.

The class has three fields: next, prev, and last. The next field is the node that succeeds the current node (comes after "this" node) and the prev field is the node that precedes the current node (comes before "this" node). The last field references the node that is the tail of the linked list, that is, it's the last node in the linked list that the current node (this node) is part of.

Notice how "this node" refers to the current node which means it's whatever particular node instance we're calling methods on. So, the expression if (this == last) literally is saying "if this node object is the same object that the last field refers to" -- in other words, it's saying "if this is the tail of the linked list".

The if-statement on lines 20-22 updates the last field to the node n if the current node is the last node of the linked list. This is why. If the current node is the last node in the linked list, then this == last will be true. Given a new node, n, that will then come after the current node (this node) in the linked list, then the current node will no longer be the last node in the linked list, it will be the node that comes before the new last node, n. That is, when we set node n to be the node that comes after the current node ("this" node), then node n will effectively become the last node in the linked list. Therefore, we need to assign n to the last field to reflect that new status.

Hopefully, that clears the matter of this == last for you. You should note, as already mentioned, that this is a very poor object-oriented design since the "knowledge" of what are the head and tail nodes of a linked list should not be managed this way but in a LinkedList class instead. The Node class would ideally be a nested class of LinkedList and probably shouldn't even be accessible outside of the LinkedList class.

But this is the design you have to study and work with so you'll have to make the best of a poor design.


Lets create a constructor of the class:
I have few doubts:

If i create an objcect of LinkListNode class like that:
   
then,  for object  first,
1.  data = 0;
2. In setNext method, next = null;  after that, if(this == last) , if last is equal to null, then this refers to what value of first ofject of LinkedListNode and why ?
   
3 days ago

Campbell Ritchie wrote:The node shouldn't be a top‑level class. It has no existence on its own, merely constituting part of a larger structure.
Did you copy that code from somewhere else or write it yourself? If somewhere else, please tell us




Source: Cracking the coding interview, Gayle McDowell, 6th edition, Appendix
3 days ago
I am having difficulty getting the concepts of the code below. In  setNext method of above code, once we set next = n, next gonna contain the address of n - in my understanding. However, I didn't understand the setNext method when it says this == last, I didn't get it atall.  

5 days ago

Campbell Ritchie wrote:

Md Zuanyeed Kamal wrote:. . . page no 93 of 6th edition of this  her book, . . .

Please supply the author's name.

Please practise drawing the structure of a linked list, and practise writing its simple operations. Make sure to give it a private nested Node class and to maintain references to the head Node. Start with a singly‑linked list and then enhance it to a doubly‑linked list, which has a tail Node as well as a head. Then you will understand what the quote from the book means.

Please use the quote button, not green text; some people can only read certain colours.



Author: Gayle Laakmann McDowell 6th edition.

I will try to implement a simple singly linklist. lets see, if i can get the concepts.
6 days ago

Junilu Lacar wrote:I believe we've seen this design before. If this Node class was encapsulated in a LinkedList class, then managing the head of the list this way wouldn't be too bad. However, if this were a top-level class where client code had to keep a reference to the head of a list, then it's not a very good design, in my opinion. I would even guess that it's something that some instructor took from a non-object oriented language and tried to directly translate it to Java. If that was the case, then the instructor was being lazy at best.



you can consider the deleteMethod is in different class.



Node deleteNode(Node head, int d) {
Node n = head;

if (n.data == d) {
return head.next; /* moved head*/
}

while (n.next != null) {
if (n.next.data == d) {
n.next = n.next.next;
return head; /* head didn't change*/
}
n = n.next;
}
return head;
}

1 week ago

Campbell Ritchie wrote:Why would a delete method return a Node in the first place? Shouldn't it be called remove() to match the remove() method in java.util.List? Wouldn't it return the value removed, so its return type would be int? Does that description say anything about the potential inefficiency of iterating a linked list?



page no 93 of 6th edition of this  her book, it was mentioned :

Deleting a node from a linked list is fairly straightforward. Given a node n, we find the previous node prev and set prev. next equal to n. next. If the list is doubly linked, we must also update n. next to set n. next. prev equal to n. prev. The important things to remember are (1) to check for the null pointer and (2) to update the head or tail pointer as necessary.



But I didn't understand it clearly.


[edit]Change green text to quote block.
1 week ago
I was reading a book " cracking the coding interview" wherein I came across the code below : I didn't understand why would i have to return head.next in line 12 ? In line 17 we are returning the head but we are updating Node n, not head. Shouldn't I return n instead of head  ?

1 week ago

Carey Brown wrote:



Initally, I was trying count the frequency of characters in a string. because i was trying to compare two list if string. My input argument was (List<String> list1, List<String>list2)then I have to comapre string by string from both the list based on character frequency. If you can fit in List<String>, then it might work.But I couldn't do that way.
3 weeks ago

Piet Souris wrote:As Paul explained, in line 2 you set your String[] ultimate to null. Next you use that same null array in line 7, and that gives you this NPE. Save is to set the String[] to an initial size of list1.size(), like in:

Does the method 'compareMapping' return a String?


it worked. yeah, it returns string // here, signature of the method:  String compareMapping(String, String)
I declared the string as null, then I tried to access it. lol. Sometimes I do  mistake. Good thing is that i am learning from my mistake. thanks all of you for correction.
3 weeks ago

Piet Souris wrote:Beware that the two maps may differ in the keys.

And to add to Knute's forEach: you can do the exercise in one line, using the Map.merge() method.



for the sake of simplicity , i considered two list are same in size. I am facing one more problem. I am getting null pointer exception even though I populated the list with data.



thank you
3 weeks ago

Paul Clapham wrote:Well, yeah. Look at line 2. And then of course you're going to get a NPE at line 7.



I didnt get it. Actually, I already populated  both the list with string  data. ultimate array should have string data according to line 7
3 weeks ago

Piet Souris wrote:
If you get your code up and running, can you explain the idea behind the subtraction of the values?



I was trying to implement a function :


Here, I have to check:
# of times char 'a' occurs in first string of list1
# of times char  'a' occurs in first string of list2
--------------------------------------------------
--------------------------------------------------
until # of times char 'z' occurs in first string of list1
# of times char  'z' occurs in first string of list2

if all differences are less than a certain number like  3 for all digits, then both the string are similar, otherwise it is not.
later on, I  have to compare   all of the strings for both the list based on character.
In order to find out number of times a character occurs, I used HashMap .


Still I am getting null pointer exception in the code below:





thank you
3 weeks ago

Piet Souris wrote:I gave you a solution,  just read my reply. Note that a Collection does not have a get method, so you must use two iterators.


Thanks I just tried the code :


but I am getting null pointer exception. Here the problem is: map keys are based on character. that is why , we can not loop through the map with int i. That's why I am getting null pointer exception
3 weeks ago