3/02/2018

Day 23 BST Level Order Traversal HackerRank Solution In C++

Problem:- Day 23 BST Level-Order Traversal hackerRank or Hackerrank: Day 23: BST Level-Order Traversal or binary search tree insertion hackerrank solution or Day 23 BST(binary search tree) Level order traversal hackerrank solution or hackerrank Day 23 BST(binary search tree) Level order traversal solution or hackerrank solution Day 23 BST(binary search tree) Level order traversal solution or hackerrank Day 23 BST Level order traversal solution or hackerrank solution BST Level order traversal solution or hackerrank solution binary search tree Level order traversal solution or (bst) binary search tree Level order traversal solution or hackerrank binary search tree Level order traversal solution.

1. What is a binary search tree(BST)?
2. Binary search tree time complexity

Now explain both queries with a full explanation. so let's see what is Binary Search Tree(BST).

1. What is a Binary Search Tree(BST)

Binary search trees (BST), sometimes called ordered or sorted binary trees, are a particular type of container: data structures that store "items" (such as numbers, names etc.).

binary search tree
credit: Wikipedia

Binary search trees keep their keys in sorted order, so that lookup and other operations can use the principle of binary search: when looking for a key in a tree (or a place to insert a new key), they traverse the tree from root to leaf, making comparisons to keys stored in the nodes of the tree and deciding, on the basis of the comparison, to continue searching in the left or right subtrees. On average, this means that each comparison allows the operations to skip about half of the tree so that each lookup, insertion or deletion takes time proportional to the logarithm of the number of items stored in the tree.

2. Binary search tree time complexity

Algorithm Average     Worst case
Space O(n)         O(n)
Search O(log n)         O(n)
Insert O(log n)         O(n)
Delete O(log n)         O(n)

Read complete article on Wikipedia

Task:- A level-order traversal, also known as a breadth-first search, visits each level of a tree's nodes from left to right, top to bottom. You are given a pointer root pointing to the root of a binary search tree. Complete the levelOrder function provided in your editor so that it prints the level-order traversal of the binary search tree.

Hint: You'll find a queue helpful in completing this challenge.

Input Format

  • The locked stub code in your editor reads the following inputs and assembles them into a BST:
  • The first line contains an integer, T (the number of test cases). 
  • The T subsequent lines each contain an integer, data, denoting the value of an element that must be added to the BST.


Output Format

Print the data value of each node in the tree's level-order traversal as a single line of N space-separated integers.

Sample Input

6
3
5
4
7
2
1

Sample Output

3 2 5 1 4 7

Explanation

The input forms the following binary search tree:

Binary search tree example
image Credit:- HackerRank

We traverse each level of the tree from the root downward, and we process the nodes at each level from left to right. The resulting level-order traversal is 3-->2-->5-->1-->4-->7, and we print these data values as a single line of space-separated integers.

Copy the colored code and paste it into Hacker rank editor and click to submit button, but before this process, you have to click the below link, this link takes to you same problem Now you have to just Login if you are already not log in and done. 

Submit this solution:- Click Here

Solution:-

#include <iostream>
#include <cstddef>
#include <queue>
#include <string>
#include <cstdlib>

using namespace std;
class Node{
    public:
        int data;
        Node *left,*right;
        Node(int d){
            data=d;
            left=right=NULL;
        }
};
class Solution{
    public:
  Node* insert(Node* root, int data){
            if(root==NULL){
                return new Node(data);
            }
            else{
                Node* cur;
                if(data<=root->data){
                    cur=insert(root->left,data);
                    root->left=cur;
                }
                else{
                   cur=insert(root->right,data);
                   root->right=cur;
                 }           
           return root;
           }
        }
       
    
  void levelOrder(Node * root){
    queue<Node *> q;
    Node* n = root;
   
    while(n != NULL){
        cout << n->data << ' ';
        
        if( n->left  != NULL ) q.push(n->left);
        if( n->right != NULL ) q.push(n->right);

        if( !q.empty() ) {
        n = q.front();
        q.pop();
        } else {
        n = NULL;
        }
    }
}


};//End of Solution
int main(){
    Solution myTree;
    Node* root=NULL;
    int T,data;
    cin>>T;
    while(T-->0){
        cin>>data;
        root= myTree.insert(root,data);
    }
    myTree.levelOrder(root);
    return 0;
}

Output:-

Binary search Tree Level Order Traversal HackerRank Solution Output


You May Also Check

1. Day 24: More Linked Lists

2. Day 25: Running Time and Complexity

3. Day 26: Nested Logic

4. Day 27: Testing

5. Day 28: RegEx, Patterns, and Intro to Databases
Read More »

3/01/2018

Day 21 Generics HackerRank Solution In C++

Problem:- Day 21 Generics hacker rank solution or HackerRank-30-Days-of-Code/Day 21 Generics or Day 21 Generics/HackerRank-30-Days-of-Code or Day 21 Generics 30 Days of Code solution or Generics HackerRank solution in c or HackerRank Generics solution in c or Generics solution hacker rank or Generics solution hacker rank in c++ or Generics solution in c++ hacker rank or Day 21: Generics | HACKER RANK SOLUTIONS or Hackerrank Day 21: Generics.

Task:- Write a single generic function named printArray; this function must take an array of generic elements as a parameter (the exception to this is C++, which takes a vector). The locked Solution class in your editor tests your function.

Note: You must use generics to solve this challenge. Do not write overloaded functions.

Input Format

The locked Solution class in your editor will pass different types of arrays to your printArray function.

Constraints

You must have exactly 1 function named printArray.

Output Format

Your printArray function should print each element of its generic array parameter on a new line.

Explanation:- We have to just print the generic array, we can do this by using the AUTO keyword in our program. I didn't understand why people don't use AUTO Keyword in C++. as you can see that generic array means all type of data type in an array so for that this is a best practice to use the AUTO keyword in the problem.

Copy the colored code and paste it into Hacker rank editor and click to submit button, but before this process, you have to click the below link, this link takes to you same problem Now you have to just Login if you are already not log in and done. 

Submit this solution:- Click Here

Solution:-

#include <iostream>
#include <vector>
#include <string>

using namespace std;

template <class T> 
    void printArray(vector<T> i) 

    for(int j=0;j<i.size();j++) 
        cout<<i[j]<<endl;



void printArray(vector<auto> a)
{
    for(auto i:a)
        cout<<i<<endl;
}


int main() {
int n;

cin >> n;
vector<int> int_vector(n);
for (int i = 0; i < n; i++) {
int value;
cin >> value;
int_vector[i] = value;
}

cin >> n;
vector<string> string_vector(n);
for (int i = 0; i < n; i++) {
string value;
cin >> value;
string_vector[i] = value;
}

printArray<int>(int_vector);
printArray<string>(string_vector);

return 0;
}

Output:-

Day 21 Generics HackerRank Solution output


You May Also See

1. Day 22: Binary Search Trees

2. Day 23: BST Level-Order Traversal

3. Day 24: More Linked Lists

4. Day 25: Running Time and Complexity

5. Day 26: Nested Logic
Read More »

2/28/2018

Day 25 Running Time and Complexity Hacker Rank Solution In C++

Problem:- Day 25 Running Time and Complexity hackerrank or HR-30-days-of-code/day25-Running Time and Complexity or running time and complexity hackerrank solution in c++ or running time and complexity solution or day 25 hackerrank solution or day 25 solution in c++ or hackerrank day 25 solution in c or hackerrank Day 25 Running Time and Complexity solution or hackerrank Running Time and Complexity solution

Task:- A prime is a natural number greater than 1 that has no positive divisors other than 1 and itself. Given a number,n, determine and print whether it's Prime or Not Prime.

Note: If possible, try to come up with a 0(square root(n)) primality algorithm, or see what sort of optimizations you come up with for an O(n) algorithm. Be sure to check out the Editorial after submitting your code!

Input Format

The first line contains an integer, T, the number of test cases. 
Each of the T subsequent lines contains an integer,n, to be tested for primality.

Output Format

For each test case, print whether n is Prime or Not Prime on a new line.

Sample Input

3
12
5
7

Sample Output

Not prime
Prime
Prime
Explanation

Test Case 0: n = 12.

12 is divisible by numbers other than 1 and itself (i.e.: 2, 3, 6 ), so we print Not Prime on a new line.

Test Case 1: n = 5.

5 is only divisible 1 and itself, so we print Prime on a new line.

Test Case 2: n = 7.

7 is only divisible 1 and itself, so we print Prime on a new line.

Submit this solution:- Click Here

Solution:-

#include <bits/stdc++.h>
using namespace std;

bool primeornot(int );

int main() 
{
    int n, i;
    bool f;
    cin >> n;
    
    vector<int> arr(n);
    for(i = 0; i < n; ++i)
    {
        cin >> arr[i];
        bool f = primeornot(arr[i]);
        
        if(f)
        {
            cout<<"Prime"<<endl;
        }
        else
        {
            cout<<"Not prime"<<endl;
        }
    }    
    return 0;
}

bool primeornot(int n)
{
    int i ,sqr;
    if(n == 1)
    {
        return false;
    }
    if(n == 2)
    {
        return true;
    }
    sqr = sqrt(n);
    for(i = 2; i <= sqr; ++i )
    {
        if(n % i == 0)
        {
            return false;
        }
    }
    return true;
        
}

Output:-

Running Time and Complexity Solution In C++

You May Also See

1. Day 26: Nested Logic

2. Day 27: Testing

3. Day 28: RegEx, Patterns, and Intro to Databases

4. Day 29: Bitwise AND

5. Day 0: Hello, World.
Read More »

2/27/2018

Day 27: Testing Hacker Rank Solution In C++

Problem:- day 27: testing java or day 27: testing c++ or day 27: testing c++ or hacker-rank-30-days-of-code/Day27.java or day 27: testing hacker rank solutions or day 27: testing hacker rank solutions in c++ or hacker rank day 27: testing solutions or hacker rank day 27: solutions or Hackerrank 30 Days of Code Challenges solutions or day 27: solutions in C.

Explanation:- A Discrete Mathematics professor has a class of n students. Frustrated with their lack of discipline, the professor decides to cancel class if fewer than k students are present when class starts. Given the arrival time of each student, determine if the class is canceled.

Input Format

The first line of input contains t, the number of lectures.

The information for each lecture spans two lines. The first line has two space-separated integers, n (the number of students in the class) and k (the cancelation threshold). The second line contains n space-separated integers describing the array of students' arrival times (A = a0, a1, a2, . . . ,a(n-1)). 


Note: Non-positive arrival times (a(i)<=0) indicate the student arrived early or on time; positive arrival times (a(i)>0) indicate the student arrived a(i) minutes late. If a student arrives exactly on time ( a(i) = 0), the student is considered to have entered before the class started.

Output Format

For each test case, print the word YES if the class is canceled or NO if it is not.

Example

When properly solved, this 

input:

2
4 3
-1 -3 4 2
4 2
0 -1 2 1

Produces this 

Output:

YES
NO

For the first test case, k = 3. The professor wants at least 3 students in attendance, but only 2 arrive on time (-3 and -1). Thus, the class is canceled.


For the second test case, k = 2. The professor wants at least 2 students in attendance, and 2 to arrive on time (0 and 1). Thus, the class is not canceled.

Now there is contradiction according to this problem my first solution should work but I don't know why HackerRank is not accepting my solution and I tried much time but didn't succeed after a time I decide to read discussion section then I found a solution so for submitting solution you need to submit solution 2 and remember one thing there are no test cases for this question so you have to put custom test cases. The custom test case is below.

Custom Test Cases.

5
5 3
-1 90 999 100 0
4 2
0 -1 2 1
3 3
-1 0 1
6 1
-1 0 1 -1 2 3
7 3
-1 0 1 2 3 4 5

Submit this solution:- Click Here

Solution:-

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int T,i,n,k,count;
    int arr[200];
    cin>>T;
    while(T--)
    {
        count=0;
        cin>>n>>k;
        
        for(i = 0; i < n; i++)
        {
            cin>>arr[i];
        }
        for(i = 0; i < n; i++)
        {
            if(arr[i]>=0)
            {
                count++;
            }
        }
        if(count>=k)
        {
            cout<<"No"<<endl;
        }
        else
        {
            cout<<"Yes"<<endl;
        }
    }
}


Solution 2 (Submit This Solution)

#include<bits/stdc++.h>
using namespace std;

int main()
{
    cout<<"5"<<endl; 
    cout<<"5 3"<<endl; 
    cout<<"-1 90 999 100 0"<<endl;
    cout<<"4 2"<<endl; 
    cout<<"0 -1 2 1"<<endl;
    cout<<"3 3"<<endl; 
    cout<<"-1 0 1"<<endl;
    cout<<"6 1"<<endl; 
    cout<<"-1 0 1 -1 2 3"<<endl;
    cout<<"7 3"<<endl; 
    cout<<"-1 0 1 2 3 4 5"<<endl;   
}

Output:-

Day 27 Hacker Rank Solutions

Day 27 Testing Solution In C++


You May Also See

1. Day 28: RegEx, Patterns, and Intro to Databases

2. Day 29: Bitwise AND

3. Day 0: Hello, World.

4. Day 1: Data Types

5. Day 2: Operators
Read More »

Day 22 Binary Search Trees HackerRank Solution In C++

Problem:- day-22-binary-search-tree or Day 22 Binary search Tree hackerrank or Hacker Rank - Day 22 Binary search Tree or Hackerrank Day 22 Binary search Tree or BST(Binary search Tree) hackerrank or Day 22 BST(binary search tree) hackerrank solution or hackerrank Day 22 BST(binary search tree) solution or hackerrank Day 22 Binary search Tree solution or hackerrank solution BST (Binary Search Tree) solution or hackerrank solution binary search tree solution or hackerrank binary search tree solution

Binary Search Tree(BST):- I have explained everything about BST in next tutorial you can find a link to next tutorial at the end of this tutorial. 

1. Binary search Tree time complexity

Algorithm Average     Worst case
Space O(n)     O(n)
Search O(log n)     O(n)
Insert O(log n)     O(n)
Delete O(log n)     O(n)


Task:- The height of a binary search tree is the number of edges between the tree's root and its furthest leaf. You are given a pointer, root, pointing to the root of a binary search tree. Complete the getHeight function provided in your editor so that it returns the height of the binary search tree.

Input Format

  • The locked stub code in your editor reads the following inputs and assembles them into a binary search tree: 
  • The first line contains an integer, n, denoting the number of nodes in the tree. 
  • Each of the n subsequent lines contains an integer, data, denoting the value of an element that must be added to the BST.


Output Format

The locked stub code in your editor will print the integer returned by your getHeight function denoting the height of the BST.

Sample Input

7
3
5
2
1
4
6
7

Sample Output

3

Copy the colored code and paste it into Hacker rank editor and click to submit button, but before this process, you have to click the below link.

Submit this solution:- Click Here

Solution:-

#include <iostream>
#include <cstddef>

using namespace std;

class Node{
    public:
        int data;
        Node *left;
        Node *right;
        Node(int d){
            data = d;
            left = NULL;
            right = NULL;
        }
};
class Solution{
    public:
  Node* insert(Node* root, int data) {
            if(root == NULL) {
                return new Node(data);
            }
            else {
                Node* cur;
                if(data <= root->data){
                    cur = insert(root->left, data);
                    root->left = cur;
                }
                else{
                    cur = insert(root->right, data);
                    root->right = cur;
               }

               return root;
           }
        }
         
          int getHeight(Node* root){
          //Write your code here
          if(!root) {
              return -1;
          }
          int leftDepth = getHeight(root->left);
          int rightDepth = getHeight(root->right);
              
          return (leftDepth > rightDepth ? leftDepth : rightDepth) + 1;
      }
      
      }; //End of Solution
int main() {
    Solution myTree;
    Node* root = NULL;
    int t;
    int data;

    cin >> t;

    while(t-- > 0){
        cin >> data;
        root = myTree.insert(root, data);
    }
    int height = myTree.getHeight(root);
    cout << height;

    return 0;
}

Output:-

Day 22 Binary Search Trees HackerRank Solution Output


You May Also See

1. Day 23: BST Level-Order Traversal

2. Day 24: More Linked Lists

3. Day 25: Running Time and Complexity

4. Day 26: Nested Logic

5. Day 27: Testing
Read More »

2/26/2018

Day 24 More Linked Lists HackerRank Solution In C++

Problem:- Day-24-More-Linked-List solution or HackerRank/Day 24: More Linked Lists or Day 24 More Linked Lists solution or Day 24 More Linked Lists hackerrank or hackerrank More Linked Lists or Day 24 More Linked Lists java or HR-30-days-of-code/Day 24 More Linked Lists or More Linked Lists hackerrank solution in c++ or day 24 hackerrank solution or day 24 solution in c++ or hackerrank day 24 solution in c or hackerrank Day 24 More Linked Lists solution or hackerrank Running Time and Complexity solution

Task:- A Node class is provided for you in the editor. A Node object has an integer data field, data, and a Node instance pointer, next, pointing to another node (i.e.: the next node in a list).

A removeDuplicates function is declared in your editor, which takes a pointer to the head node of a linked list as a parameter. Complete removeDuplicates so that it deletes any duplicate nodes from the list and returns the head of the updated list.

Note: The head pointer may be null, indicating that the list is empty. Be sure to reset your next pointer when performing deletions to avoid breaking the list.

Input Format

You do not need to read any input from stdin. The following input is handled by the locked stub code and passed to the removeDuplicates function:

The first line contains an integer, N, the number of nodes to be inserted.

The N subsequent lines each contain an integer describing the data value of a node being inserted at the list's tail.

Constraints

The data elements of the linked list argument will always be in non-decreasing order.

Output Format

Your removeDuplicates function should return the head of the updated linked list. The locked stub code in your editor will print the returned list to stdout.

Sample Input

6
1
2
2
3
3
4

Sample Output

1 2 3 4

Explanation

N = 6, and our non-decreasing list is {1, 2, 2, 3, 3, 4}. The values 2 and 3 both occur twice in the list, so we remove the two duplicate nodes. We then return our updated (ascending) list, which is {1, 2, 3, 4}.

Tip:- Copy the colored code and paste into HackerRank Editor.

Submit this solution:- Click Here

Solution:-

#include <cstddef>
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class Node
{
    public:
        int data;
        Node *next;
        Node(int d){
            data=d;
            next=NULL;
        }
};
class Solution{
    public:
Node* removeDuplicates(Node *head)
{
    if (!head)
        return head;
    
    Node *node = head;
    while (node->next) {
        if (node->data == node->next->data) 
            node->next = node->next->next;
        else
            node = node->next;
    }
    
    return head;
}
          Node* insert(Node *head,int data)
          {
               Node* p=new Node(data);
               if(head==NULL){
                   head=p;  

               }
               else if(head->next==NULL){
                   head->next=p;

               }
               else{
                   Node *start=head;
                   while(start->next!=NULL){
                       start=start->next;
                   }
                   start->next=p;   

               }
                    return head;
                
            
          }
          void display(Node *head)
          {
                  Node *start=head;
                    while(start)
                    {
                        cout<<start->data<<" ";
                        start=start->next;
                    }
           }
};

int main()
{
Node* head=NULL;
  Solution mylist;
    int T,data;
    cin>>T;
    while(T-->0){
        cin>>data;
        head=mylist.insert(head,data);
    }
    head=mylist.removeDuplicates(head);

mylist.display(head);



Output:-
Day 24 More Linked Lists Solution In C++

You May Also See

1. Day 25: Running Time and Complexity

2. Day 26: Nested Logic

3. Day 27: Testing

4. Day 28: RegEx, Patterns, and Intro to Databases

5. Day 29: Bitwise AND
Read More »

2/25/2018

Rectangle Area HackerRank Solution In C++

Problem:- HackerRank/rectangle-area.cpp or Rectangle Area Leaderboard or Rectangle Area solutions or Rectangle area hackerRank solution in c++ or rectangle area solutions hackerRank or rectangle area solutions in c++ or hackerRank rectangle area solutions in c++ or hackerRank solutions c++.

Explanation:- Create two classes:

Rectangle

The Rectangle class should have two data fields-width and height of int types. The class should have display() method, to print the width and height of the rectangle separated by space.

RectangleArea

The RectangleArea class is derived from Rectangle class, i.e., it is the sub-class of Rectangle class. The class should have read_input() method, to read the values of width and height of the rectangle. The RectangleArea class should also overload the display() method to print the area (width * height) of the rectangle.

Input Format

The first and only line of input contains two space-separated integers denoting the width and height of the rectangle.

Constraints

1<= width, height<=100

Output Format

The output should consist of exactly two lines: In the first line, print the width and height of the rectangle separated by space. In the second line, print the area of the rectangle.

Sample Input

10 5

Sample Output

10 5
50

Here, width = 10 and height = 5, so area = width * height = 50.

Copy the colored code and paste into the editor.

Submit this solution:- Click Here

Solution:-

#include <iostream>
using namespace std;

class Rectangle
{
  protected:
  int width;
  int height;
    public:
    void read_input()
    {
        cin>>width>>height;
    }
     void display()
    {
    cout<<width<<" "<<height<<"\n";
    } 
};
class RectangleArea :public Rectangle
{
    public:
      void display()
    {
      cout<<width * height;
    }
};

int main()
{
    /*
     * Declare a RectangleArea object
     */
    RectangleArea r_area;
    
    /*
     * Read the width and height
     */
    r_area.read_input();
    
    /*
     * Print the width and height
     */
    r_area.Rectangle::display();
    
    /*
     * Print the area
     */
    r_area.display();
    
    return 0;
}

Output:-


Rectangle Area Solutions HackerRank

You May Also See

1. Hacker Rank Solution For Birthday Cake Candles

2. C++ Program For Store Employee Information And Display Using Structure

3. C Program For Find A Grade Of Given Marks Using Switch Case

4. C Program For HEAP Sort In Ascending Order Also Show Complexity

5. C++ Program For Selection Sort Using Function
Read More »

Inheritance Introduction HackerRank Solution in C++

Problem:- Inheritance Introduction Hacker Rank Problem Solution or HackerRank/inheritance-introduction.cpp or Inheritance Introduction HackerRank Solution in C++ or Inheritance Introduction HackerRank Solutions or Inheritance Introduction solutions HackerRank or hacker rank solutions in c or hacker rank solutions Inheritance Introduction or hacker rank solutions or Inheritance Introduction solutions.

Explanation:- One of the important topics of Object Oriented Programming is Inheritance. Inheritance allows us to define a class in terms of another class, which allows us in the reusability of the code.Check out the code below:

class Triangle{
    public:
        void triangle(){
            cout<<"I am a triangle\n";
        }
};

The class Triangle has a function called triangle(). Now we create a class derived from the base class Triangle called Isosceles.

class Isosceles: public Triangle{
    public:
        void isosceles(){
            cout<<"I am an isosceles triangle\n";
        }
};

Now we can create a derived class object and use it to access the functions of the base class.

int main(){
    Isosceles isc;
    isc.isosceles();
    isc.triangle();
    return 0;
}

The output of this code will be.

I am an isosceles triangle
I am a triangle

Now write a function in Isosceles class such that the output is as given below.

Sample Output

I am an isosceles triangle
In an isosceles triangle two sides are equal
I am a triangle

Submit your solution here:- Click Here

Tips:- Copy the full code and paste Hacker Rank editor or you can copy the colored code and paste below "//Write your code here." in Hacker Rank editor.

Solution:-

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

class Triangle
{
    public:
       void triangle()
   {
           cout<<"I am a triangle\n";
       }
};
class Isosceles : public Triangle
{
    public:
       void isosceles()
   {
          cout<<"I am an isosceles triangle\n";
       }
        //Write your code here.
        void description()
{
          cout<<"In an isosceles triangle two sides are equal\n";
        }
};
int main()
{
    Isosceles isc;
    isc.isosceles();
    isc.description();
    isc.triangle();
    return 0;
}

Output:-

Inheritance Introduction solutions


You May Like This:-

1. Hacker Rank Solution for 30 Days of Code

2. Hacker Rank solution for Attribute Parser

3. Java Program To Find The Gross Salary Of An Employee

4. Hacker Rank Solution For Conditional Statements

5. Hacker Rank Solution For " For Loop "
Read More »

1/30/2018

Guide for Execution of Custom Entity Kaleo Workflow in Liferay DXP Including Coding Part

Title: Custom Entity Kaleo Workflow Integration in Liferay DXP Including Coding Part Workflow Engines have their own precise patterns starting from verification of the current status to determining the authority of users to executing condition script. The developer’s methodology to integrate it with the application defines the critical success factor of its optimum usability. Hereby, I am sharing one of my experiences of implementing Kaleo workflow engine in a Liferay development process.

Liferay supports Kaleo workflow engine already deployed in its server bundle as well as available at Liferay marketplace. Not only that, Kaleo substantially has out of the box unification with Liferay DXP’s functional characteristics for the creation of roles, users, and organizations. Hence, I opted for Kaleo for the project we are discussing as the used case.

Project Details:


  • A Liferay Portal Development project for building a domain-specific web-based collaboration platform similar to PeoplePerHour.com or Guru.com

  • User Details:
liferay kaleo workflow tutorial

Kaleo Workflow with custom entity "ABCTask"

To implement the above-described workflow in my Liferay Application Development process, I created a custom entity "ABCTask" followed by workflow inception programmatically. I preferred dynamic navigation from one node to other instead of using UI provided by Liferay DXP.

How to code for actual execution of Kaleo workflow


Know your Nodes first before you connect them

As a developer, we consider workflow to be a sensible connection between different nodes. Nodes can be:

liferay kaleo workflow tutorial


How to create Workflow Definition?


Creation of workflow definition (XML file) is an in-depth topic. I would suggest you be very clear about it. You can refer the following link for the same.

Link:- https://dev.liferay.com/discover/portal/-/knowledge_base/6-2/creating-new-workflow-definitions 


Coding Details


As developers, we are aware of the fact that when it comes to executing a workflow, all nodes are a task.

[A] Creation of Workflow Instance

When User A creates ABCTask, we need to start workflow instance of "ABCTask" entity using Workflow API provided by Liferay. The coding part is displayed below. 

 Create Workflow Definition Link such as:

liferay kaleo workflow tutorial

To enable a workflow for the custom entity, we have to make that entity an asset:

Custom Entity Kaleo Workflow in Liferay DXP

Start workflow instance:


Custom Entity Kaleo Workflow in Liferay DXP

When the workflow gets started, it will be the initial state, known as the CREATED state. The workflow will be executed to the Task node of the workflow. The user input will be required at Task node.

[B] Obtaining Workflow Task

When User B sends the proposal to User A and User A approves it, the state of Entity has to be moved from “Start” to “Approved”. Liferay’s API can be used for moving workflow task from one state/task to other state/task.

Of course, without completion of existing workflow task, you cannot move to another Task node. You can obtain Workflow Task from workflow logs. Please find the code snippet to obtain the workflow task and complete that task below

Workflow Instance is required to manage workflow operations:

Custom Entity Kaleo Workflow in Liferay DXP

[C] Obtaining the latest workflow taskId from logs:

Custom Entity Kaleo Workflow in Liferay DXP

[D] Completion of the Workflow Task:

You need next transition name to execute other nodes.


liferay dxp workflow


Conclusion:


Though technology world offers us unlimited tools to create business-friendly software applications, optimum utilization of Workflow Engines for desired process management demands individual creativity from a developer. This project enhanced my individual skills as a developer by driving my creative logic to the next level.  


Author of This Post


Author Bio: Namee Jani is a Passionate Digital Marketing Strategist working with Azilen Technologies. She is Certified Google Adwords & Analytics and adores catching new trends and techniques prevalent in the online marketing world. Click Here for more information about Author
Read More »