I saved a json into the database as a string like this: “[_district:_1_2_5village, _name:_1_1_2id_inter, _gender:_1_3_5sex]” Now i want to convert it back to a Json Object so as to pick the key and value eg _district is the key and _1_2_5village is the value. Any help on how i can achieve this. Th…
Tag: data-structures
Adding two linked list ends in Infinite Loop
I’m trying to solve LeetCode problem 2. Add Two Numbers: You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. You may assume the t…
Add 2 list of objects and remove duplicate(all but one field different) elements from the final list
I have a object class like I have 2 lists 1st one is like {d1,m1,active}, {d2,m2,active},{d3,m3,acticve} 2nd one {d2,m2,paused},{d4,m4,paused} i want my final list to be like {d1,m1,active},{d2,m2,paused},{d3,m3,active},{d4,m4,paused} My approach was to make a list of commonName{d2} then add both the list{d1,…
How to implement Breadth First Search and Depth First Search for a SimpleWeightedGraph in Java
I need to implement Breadth First Search and Depth First Search for a SimpleWeightedGraph which contains String Vertices in Java Code below is my SimpleWeightedGraph where i implemented the following: declared SimpleWeightedGraph created vertices added vertices to the graph created default weighted edges, add…
Initialize a Linked List nodes using map().reduce()
I want to do this initialization of the Node class using java streams. How do I do this using the map and reduce stream operations? I’ve tried something like this, which does not compile I want to map each element in the array to Node(int i , Node n) and then reduce it to a Node. What am I missing
How does parameters of a method get stored in stack during a recursive call?
I was doing a leetcode question https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree-ii/ and I am confused on how parameters for a method get stored during a recursive call. If a node gets visited, I wanted to save it’s state that it was visited. When I send two variables, when the stac…
Method to get all the branches of a tree
I have an object from this Class: And I would like to write the JAVA method: private List<List> extractNames(Person ancestor) which gives back all the names of each branch of the tree: Do you have an idea, how I could do it ? Answer Update: Removed the dependency to lombok The relevant algorithm part is…
Checking a tree to be a BST
Here is my attempt to check whether a tree is a BST or not: Code works fine as tested with multiple test cases. But I am not sure if this is a good, clean approach. Recursive method is big it seems. I am dealing with scenarios like null left node, null right node, node itself null, both child nodes null
Java – HashMap for booking system
I have been given an exercise to develop an appropriate data structure to implement a tennis court booking system. A hashmap is what is required for this and I was given a diagram as a visual representation of the data structure. From looking at the diagram, I am unclear on how the Key and Value should be def…
Function to check for perfect square not working for large number
I came across an algorithm to tell if a given number is perfect square or not in O(logN) time. Here is the implementation(JAVA) of the idea. This works fine for numbers like 256, 808201 , etc But fails for numbers like 999966000289. I cannot figure out why? Answer As mentioned in the comments, the problem is …