Wednesday, October 11, 2017

[Problem2 SPOJ-JNEXT] Next Lexicographical Permutation Algorithm; Ad-Hoc


I was solving SPOJ-JNEXT, and from the comments section I came across this page:

Nice explanation but there's a caveats to keep in mind to understand the working better-

It mentions:
If there is no such element – i.e. the entire sequence is non-decreasing – then this is already the last permutation.
It should rather be "non-increasing" instead of "non-decreasing".

My approach to solving this problem was similar so I won't bore you with another copy of how the algorithm works here: you may use the explanation in link above (with the correction in mind).

Tested and accepted:,chandniverma/

Time Complexity of get_next_perm() in worst case: O(n) where n is the number of digits in each test case in the input.
Space required: O(n)

Tuesday, October 10, 2017

[Problem1 SPOJ:STPAR] Stacks use case; Ad-hoc

Starting last weekend I have started picking up problems from the certification syllabus of the Codechef Certified Data Structure & Algorithms Programme: CCDSAP ( ). I would definitely recommend solving the practice material even if you are not willing to go for the certification.

I'll solve these in increasing order of difficulty and not necessarily sequentially while skipping the cake-walk ones and also probably the ones that I have done previously.
This way I can ensure this series of self-paced hackathon-cum-blogathon will eventually come to an end, with an end to the problems in this syllabus.


I had done this problem earlier so it was a good candidate for an ice-breaker. I first (sometime in 2010) solved this in Java. This time in C++ using an on-the-fly algorithm that doesn't store all inputs before processing them but processes each input as it comes in a single pass.

Tested and accepted at :,chandniverma/

The time complexity is O(n).
Space needed is O(n) (actually only n).

Friday, October 6, 2017

Blogging after a long time... turning my blog into an algorithmist's blog solving this world's time and memory efficiency problems in computing.

... Since I cannot talk much about what things I work on as part of my day job and since I feel that's making my blog boring and making the viewers wait a lot between my blogging sessions, I've decided to start blogging about algorithmic programming skills which every computer software engineer should be good at!

I'll do this because I like spending my free time in sharpening my algorithmic skills and while I take notes, why not should someone-on-the-web learn something out of it?

I'll post codes into a Github public repository which will have C++ solutions (why C++? due to it's speed, presence of STL and intuitiveness of Object Orient-ism) tested by online judges and if I learn something triggering an Aha moment, I'll jot down a quick blog post with links to an actual runnable code and the problem it solves.

You all can send thank you notes and suggestions for improvements my way, as always!

Sounds fun?
Watch this space!

Thursday, November 17, 2016

Making lshw, IBM PowerPC compliant

Dear Linux Community,

The first tool that comes to mind when we think of obtaining hardware inventory on a computer is lshw, a concise tool leveraging C++ capabilities which already generates good results when run on a number of platforms, including x86.

lshw, like other cross-platform tools has generic code portions which are same across all platforms and some platform-specific code portions.

I am glad to tell that efforts to make lshw display correct and accurate platform-specific results when run on IBM PowerPC server systems running Linux are paying off as code-changes are getting merged upstream:

My other notable commits(despite of not retaining the Author name and signoff tag) include:

I'd like to thank lshw maintainer, Lyonel Vincent, here for acknowledging and accepting the changes.

Research is ongoing on abilities that are required but are lacking in the generic code as well and some of those require deep knowledge of ioctls, SCSI, SAS etc. and their workings. We're seeing how to expose more of those hardware inventory (encapsulated by certain subsystems) along with their location-codes and Vital Product Data (VPD) to the users so watch the above spaces and keep your systems updated!

Happy Hacking!

Wednesday, August 24, 2016

Learning Python!

Here are my first few basic Python programs (before my first Python assignment consuming and processing perf samples). :)

i = raw_input()
print i

T = int(raw_input())
print "Range of t is", range(T)
for i in range(T):
    print i
    ans = 42
    print "Case #%d: %d" % (i+1, ans)
T = int(raw_input())
for i in range(T):
    line = raw_input()
    N, J = line.split()
    N, J = int(N), int(J)
    print "N = %d, J = %d" % (N, J)   

T = int(raw_input())
i = 1
while i<=T:
    tmp = raw_input()
    N = int (tmp.split()[0])
    J = int (tmp.split()[1])
    print N, J
    print 2**N
    print 2**N-1
    print bin(2**N-1)
    i +=1   

#program 5
T = int(raw_input())
i = 1
while i<=T:
    tmp = raw_input()
    N = int (tmp.split()[0])
    J = int (tmp.split()[1])
    print N, J
    print 2**N
    print 2**N-1
    print bin(2**N-1)
    s = bin(2**N-1)[2:]
    print int(s, 2)
    print int(s, 3)
    print int(s, 4)
    print int(s, 10)

#program 6
#defining functions
def factorial(n):
    ret = 1
    for i in range(n):
        ret *= i+1
    return ret
print factorial(5)
print factorial(4), factorial(3)

Friday, July 15, 2016

Secure C/C++ Coding practices

Dear Software Engineers and Amateur Programmers,

In today's scenario, writing secure code is not a choice anymore, it's a necessity.

As a result of me attending Paul Ionescu's webcast "Inside the mind of a Hacker" ( (where he talks about how crackers crack their way through your code and what loopholes and vulnerabilities they exploit) and being trained overtime with strong review comments from peers laying emphasis on secure programming, I've begun giving a keen eye to best coding practices.

One such link I googled for yesterday and thought of sharing is:

The following usage in the correctly marked answer there:
strncpy(buff, "String 1", BUFFER_SIZE - 1);
buff[BUFFER_SIZE - 1] = '\0';
is actually correct and not incorrect as pointed out by one of the commenters. See for yourself to know why!
(I couldn't add a comment there due to lack of enough points to comment on StackOverflow.)

I found many instances of insecure invocation of strncpy in the open source package I am currently working on like
strncpy(buff, "String 1", sizeof(buf));
and wanted to give a alert to the maintainers/programmers if they are using such lines often in their code so that they stop making this mistake.

Will keep posting updates in this space with more such important links.

Till then,
Cheers and Happy Coding!

Tuesday, November 24, 2015

Fetching and Extending Update Access Key expiry date for IBM's POWER8 Firmware

On IBM's POWER8 systems there was no way to have Update Access Keys updated till now.

After the updated POWER8 firmware, with my commits merged to upstream candidate "next" branch of powerpc-utils's activate_firmware utility last week, and with the ongoing efforts of the Electronic Service Agent team based on my work, it will soon be possible to make the update happen on the fly on provisioning a new update-access-key to the utility.

IBM Power8 customers will also be able to look at the current UAK Expiry date to make a decision as to when to update the system with a new key and verify whether date has been updated after providing the key or not.

Quoting the man page the options added to activate_firmware utility are:

-e [keyfile]
When used with this option, the command either fetches the current Update Access
Key expiry date or sets the Update Access Key expiry date if is

The activate_firmware utility will not directly be used by the end user but by an update_flash script to view or extend the firmware entitlement date by providing a new update-access-key.


Sunday, November 8, 2015

Adding as a remote to a local git repository, a pushable remote repository residing on a sshable server

Many people ask me how do I manage my work repositories since it is advisable to do all development on your PC and a headache to trace any change, from local repository to the test machine using scp or rsync each time a change is made. Fortunately, git has a easier way! The alternative is to add as a remote to a local git repository, a pushable remote repository residing on a sshable server. The steps are as follows:

//At the server's ssh
mkdir some_project
cd some_project/
git init
git checkout -b test #as we cannot push to a checked out branch at the server from our laptops.

//On my pc
git clone git/url/to/some_project.git 
cd some_project/
#Add pushable repo. which we inited on the server, as a remote to this local repo.
git remote add remote_name user@remote.server:/path/to/GIT/repo/some_project/
#check if added
git remote -vv
#Clear firewall to remote server, if needed
#push desired branch(es)(say master) to the init-ed blank repo on the remote server.
git push remote_name master 

//At the server ssh prompt to see the changes
git checkout master 

Voila! There's all your work!
Next time, you make any changes, in a separate branch, you just need to perform the push step at your PC and checkout the new branch on your server :D

Virtualization: virsh basic commands

virsh program is the main interface for managing virsh guest domains (or virtual machines). The program can be used to create, pause, and shutdown domains. It can also be used to list current domains. On my PC, I started off with managing new VMs using the Virtual Machine Manager GUI but when it comes to managing VMs on a headless remote server, its easier done using the text-only virsh. I find creating a fresh guest VM is the easiest using a modified xml dump from an existing VM(if one exists)

Following are some basic virsh commands I've been used to using over time-

To execute the following commands 'libvirtd' daemon should be running.

1. To see what guests are there on a particular host
virsh list 
virsh list --all

2. To define the guest
virsh define

3. To start the guest
virsh start
virsh console
virsh start --console

4. To shutdown the guest
virsh shutdown
virsh destroy

5. Get the libvirt xml
virsh dumpxml > filename.xml

6. Edit libvirt xml, with this command if there are any changes then no need to 'undefine' and 'define' the domain
virsh edit

7. To undefine the guest, CAUTION: if you want your domain back, then have domainxml back up.
virsh undefine

Cues to what basic changes are to be made to create a new VM from an existing xml-dump can be taken from the following sample xml dump:
Changes to be made are highlighted (name and UUID should be changed since they should be unique on a host, UUID tag can also be omitted altogether and a fresh one will be allotted; network highlights should match):

That's all in this primer! More on virsh and other domain commands can be read from virsh man page.